是否可以将nextState
中的react-router v3
参数复制到react-router-dom v4
?我正在尝试通过params
获取ID,但无法找到合适的方法来完成这项工作:
想迁移react-router v3
const onEnterAuthPage = (nextState) => {
if (!Meteor.userId()) {
browserHistory.replace('/');
} else {
Session.set('selectedNoteId', nextState.params.id);
}
};
export const routes = (
<Router history={browserHistory}>
<Route path="/" component={Login} onEnter={onEnterPublicPage}/>
<Route path="/signup" component={Signup} onEnter={onEnterPublicPage}/>
<Route path="/dashboard/:id" component={Dashboard} onEnter={onEnterAuthPage}/>
<Route path="*" component={NotFound}/>
</Router>
);
致react-router-dom v4
const onEnterAuthPage = () => {
if (!Meteor.userId()) {
browserHistory.replace("/");
} else {
//want to call params argument here with session
}
};
export const routes = (
<Router history={history}>
<Switch>
<Route
exact
path="/"
render={() => {
onEnterPublicPage();
return <Login />;
}}
/>
<Route
path="/signup"
render={() => {
onEnterPublicPage();
return <Signup />;
}}
/>
<Route
path="/dashboard/:id"
render={() => {
onEnterAuthPage();
return <Dashboard />;
}}
/>
<Route path="*" component={NotFound} />
</Switch>
</Router>
);
我一直在尝试解决此问题,很高兴找到解决方案。