如果您更喜欢 Redux 风格的操作,该库还提供了一组操作创建器和一个捕获它们的中间件并将它们重定向到您的历史记录实例。
for: push(location), replace(location), go(number), goBack(), goForward()
您必须安装 routerMiddleware 才能使这些操作创建器正常工作。
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'))
React Router 还提供历史记录的单例版本(browserHistory 和 hashHistory),您可以从应用程序中的任何位置导入和使用。
import { browserHistory } from 'react-router'
if(action.payload != undefined && action.payload.status==401){
browserHistory.push('login');
console.log('session expired');
}
顺便说一句,您可以使用检查身份验证onEnter https://github.com/reactjs/react-router/blob/master/docs/API.md#onenternextstate-replace-callback or redux-auth-包装器 https://github.com/mjrussell/redux-auth-wrapper