I have a small project where I am using React without Redux or Router. Also I use axios for every request.
What I need to do is that in case any response has 401 error code I need to show some kind of modal with appropriate error message.
I created an axios instance and set the response interceptor there:
const axiosInstance = axios.create({
baseURL: URL_PREFIX,
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
})
axiosInstance.interceptors.response.use(
response => response,
error => {
if(error.response.status === 401) {
// I want to show modal from here,
// but this is outside of my component tree
// and I can not access my modal state to set it open.
}
return error
}
)
Then I use my created axios instance for every API call.
I also tried to set the response interceptor from my root components useEffect() method. But it does not work
Is there any good way to accomplish this without using Redux or Router ?