How to redirect from axios interceptor with react Router V4?

后端 未结 8 2104
忘了有多久
忘了有多久 2021-01-30 17:36

I want to make a redirection in axios interceptors when receiving a 403 error. But how can I access the history outside React components ?

In Navigating Programatically

相关标签:
8条回答
  • 2021-01-30 18:42

    This works perfectly.

    window.location.href = `${process.env.REACT_APP_BASE_HREF}/login`;
    
    0 讨论(0)
  • 2021-01-30 18:42
    import { createHashHistory } from 'history' // or createBrowserHistory
    const history = createHashHistory()
    
    if (response.status === 403) {
      history.push('/login')
    }
    
    

    ReactTraining/history

    0 讨论(0)
提交回复
热议问题