Is it possible to manually add cached data?

Viewed 45

I have an api endpoint to get the configuration, because the configuration rarely changes, so I store the configuration data in localStorage;

Every time the application starts, I read the configuration from localStorage, and want to put this configuration data in the rtk query endpoint in the cache;

I tried api.util.updateQueryData, but it doesn't work, as the official documentation describes:

updateQueryData is strictly intended to perform updates to existing cache entries, not create new entries.

function App() {
  const dispatch = useDispatch()
  useEffect(() => {
    try {
      const json = localStorage.getItem('config')
      const config = json ? JSON.parse(json) : undefined
      if (config) {
        // I want this local configuration to be my initial data
        dispatch(
          api.util.updateQueryData('getConfig', undefined, () => config)
        )
      }
    } catch(error) {
      console.error(error)
    }
  }, [])
  return <div />
}

Is there any way we can manually add a cache as the endpoint's initial data? Perhaps the api.util.addQueryData?

1 Answers

There is an open Pull Request for a functionality like this but at the moment (RTK 1.8) it's not a feature.

Related