Last updated 261 days ago by Dave Ceddiaredux
Need to access your Redux store outside a React component?
It’s a bit ironic, on some level… a global store of data, but no way to access it!
The React Redux
connect function works great for regular React components, but if you need access to the Redux store in a plain function, the technique is a bit different.
In the examples below I’ll show how to access a JWT token from the Redux store, but the techniques will work with any data you might have.
This is probably the easiest option, but it has one big caveat:
DON’T USE THIS METHOD WITH SERVER SIDE RENDERING
If your app is using SSR, and you do this, you’ll end up with a SINGLE store for all of your users. Almost definitely not what you want.
So, let’s say you have a JWT token in the store, and you want to access it from your
api file. Here’s how you can do it.
Move your store creation code into its own file.
store.js is a nice name.