将react-router v3的onEnter nextState参数复制到react-router-dom v4

时间:2018-08-13 00:43:26

标签: reactjs meteor react-router-dom

是否可以将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>
);

我一直在尝试解决此问题,很高兴找到解决方案。

0 个答案:

没有答案