[英]Register a route change in React-router 1.0
Don't you know how can I register a route change in React-router 1.0? 您不知道如何在React-router 1.0中注册路由更改吗? I have tried few classic react event listeners like onChange and it's not working.
我尝试过一些经典的React事件侦听器,例如onChange,但它不起作用。 Any ideas?
有任何想法吗?
您可以尝试使用onEnter / onLeave挂钩。
If you have Redux in your project and use redux-router or redux-simple-router , you can add a middleware to capture their route change actions. 如果您的项目中有Redux并使用redux-router或redux-simple-router ,则可以添加一个中间件来捕获其路由更改操作。
This is how it can be for redux-simple-router: 这是redux-simple-router的样子:
import {createStore, applyMiddleware} from 'redux';
import {syncReduxAndRouter, routeReducer, UPDATE_PATH} from 'redux-simple-router';
function routeListenerMiddleware({ dispatch, getState }) {
return next => action => {
if(action.type === UPDATE_PATH) {
// deal with path update here.
}
next(action);
}
}
const createRouteAwareStore = applyMiddleware(routeListenerMiddleware)(createStore)
Then you have the store, which intercepts route changes. 然后便有了商店,该商店可以拦截路线更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.