Redirect using react-router in redux middleware

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 08:48:23

if you prefer Redux style actions, the library also provides a set of action creators and a middleware to capture them and redirect them to your history instance.

for: push(location), replace(location), go(number), goBack(), goForward()

You must install routerMiddleware for these action creators to work.

import { routerMiddleware, push } from 'react-router-redux'

// Apply the middleware to the store
const middleware = routerMiddleware(browserHistory)
const store = createStore(
  reducers,
  applyMiddleware(middleware)
)

// Dispatch from anywhere like normal.
store.dispatch(push('/foo'))

Also React Router provides singleton versions of history (browserHistory and hashHistory) that you can import and use from anywhere in your application.

import { browserHistory } from 'react-router'

if(action.payload != undefined && action.payload.status==401){
        browserHistory.push('login');
        console.log('session expired'); 
}

btw for check auth you may use onEnter or redux-auth-wrapper

use onEnter method of react-router. call a function which will check for the access. Example:

function checkAccess() {
   //some logic to check 
    if(notAuthorized){ window.location.href= "/login"; }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!