需求
如何利用route的onEnter钩子来出发redux的dispatch
方法
I just wrapped my routes in a function that returns them.
This allows you to pass the redux store as an argument
Any onEnter/onExit functions are also defined in there so they have access to that store argument and can dispatch() stuff.
route的配置本质上只要是下面这样,然后在放到router里就可以了。
//route.js
export default(
<Route path="/" component={Index}>
<IndexRoute component={Index} />
<Route path="/public" component={PublicArea} />
</Route>
)
//main.js
import route from './route.js'
import {Router} from 'react-router';
ReactDom.render(
<Provider store={store}>
<Router history={history}>{router}</Router>
</Provider>,
app
);
那么既然只要是像route.js
那样的格式就行了,我也可以把route.js
写成一个函数,然后用函数来返回<route>
这些路由配置就可以了。
//route.js
export default function(store){
doSth(store){
//doSth by store
}
return (
<Route path="/" component={Index}>
<IndexRoute component={Index} />
<Route path="/public" component={PublicArea} />
</Route>
)
}
//main.js
ReactDom.render(
<Provider store={store}>
<Router history={history}>{router(store)}</Router>
</Provider>,
app
);