Home > database >  how to use useSelector inside axios interceptors?
how to use useSelector inside axios interceptors?

Time:01-14

i am new to the react and i want to check whether access token has expired or not on every request using axios interceptors , but i am not able to retrieve access token stored inside the redux ( because i know we cant use hooks outside components ) so what could be the possible solution to retrieve access token stored in redux memory .

Here is my code

import axios from "axios";

const axiosInstance = axios.create({
  baseURL: "http://localhost:5000/api/v1",
});

axiosInstance.interceptors.request.use(req => {

  // get access token stored in redux

  // decode access token

  // check if token expired 

  // if expired request new access token
  
  return req ;
})

CodePudding user response:

You can import your instance of the store and the get the state.

store.getState().{reducer that contain the token}.token
  • Related