I am using an axios interceptor to handle errors globally, but would like to set the state of my app from the interceptor. Something like:
axios.interceptors.response.use(
error => {
AppState.setError(error)
}
)
function App() {
[error,setError] = useState()
}
How can I either access the state of App from outside, or alternatively how can I call a function defined within App from the interceptor?
I've tried
axios.interceptors.response.use(
error => {
App.setAppErrorState(error)
}
)
function App() {
this.setAppErrorState = function(error) {
// do something with error like set it to state
}
}
but get the error "TypeError: Cannot set property 'setAppErrorState' of undefined"
I'm not currently using Redux so a solution without using it would be preferable. Any help appreciated!