Home > database >  Can I change my Redux state from outside a function component?
Can I change my Redux state from outside a function component?

Time:09-05

I'm storing my api access token in Redux state. When my access token expires my api sends back a refreshed token as a header. What I'm trying to do is set up an axios interceptor that will change my state if a refreshed token is sent back.

const myAxios = axios.create()

myAxios.interceptors.response.use(
(response) => {
if (response.headers['x-access-token']) {
  // Is there a way to dispatch something here to change state
}

return response
},
async function (error) {
    return Promise.reject(error)
  }
)

export default myAxios

So with this I can get my token fine I just cant figure out how to change state from here since useDispatch wont work from outside a functional component

CodePudding user response:

First, export your store:

export const store = configureStore({
  ...
});

Then, import it in your file and use it:

import { store } from './store';

...
store.dispatch() // got dispatch
  • Related