【问题标题】:React Router 4 Match returns undefinedReact Router 4 Match 返回未定义
【发布时间】:2017-08-09 01:28:01
【问题描述】:

我正在使用 react 路由器 4,但无法使用参数从 url 访问 id。我遵循了 react router 4 文档,但是当我 console.log match.params.id 它返回 Cannot read property 'params' of undefined。 URL 包含 id,所以我迷路了。你可以在Path: Container找到console.log

我做错了什么?

路径:App

const App = appProps => (
  <Router>
    <div className="bgColor">
      <NavBar {...appProps} />
      <Grid className="main-page-container">
        <Switch>
          <Admin exact path="/admin/candidate_profile/:id" component={AdminCandidateProfileContainer} {...appProps} />
        </Switch>
      </Grid>
    </div>
</Router>
);

App.propTypes = {
  loggingIn: PropTypes.bool,
  authenticatedCandidate: PropTypes.bool,
  authenticatedAdmin: PropTypes.bool
};


export default createContainer(() => {
  const loggingIn = Meteor.loggingIn();
  return {
    loggingIn,
    authenticatedCandidate: !loggingIn && !!Meteor.userId() && !!Roles.userIsInRole(Meteor.userId(), 'Candidate'),
    authenticatedAdmin: !loggingIn && !!Meteor.userId() && !!Roles.userIsInRole(Meteor.userId(), 'Admin')
  };
}, App);

路径:AdminRoute

const Admin = ({ loggingIn, authenticatedAdmin, component: Component, ...rest }) => (
  <Route
    {...rest}
    render={(props) => {
      if (loggingIn) return <div />;
      return authenticatedAdmin ?
      (<Component loggingIn={loggingIn} authenticatedAdmin={authenticatedAdmin} {...rest} />) :
      (<Redirect to="/login" />);
    }}
  />
);

Admin.propTypes = {
  loggingIn: PropTypes.bool,
  authenticatedAdmin: PropTypes.bool,
  component: PropTypes.func
};

export default Admin;

路径:Container.js

export default CandidateProfileContainer = createContainer(({ match }) => {
  console.log('match', match.params.id);

  const profileCandidateCollectionHandle = Meteor.subscribe('admin.candidateProfile');
  const loading = !profileCandidateCollectionHandle.ready();
  const profileCandidateCollection = ProfileCandidate.findOne({ userId: Meteor.userId() });
  const profileCandidateCollectionExist = !loading && !!profileCandidateCollection;

  return {
    loading,
    profileCandidateCollection,
    profileCandidateCollectionExist,
    profileCandidate: profileCandidateCollectionExist ? profileCandidateCollection : {}
  };
}, CandidateProfilePage);

【问题讨论】:

    标签: reactjs react-router-v4


    【解决方案1】:

    你没有从render传递props

    const Admin = ({ loggingIn, authenticatedAdmin, component: Component, ...rest }) => (
      <Route
        {...rest}
        render={(props) => {
          if (loggingIn) return <div />;
          return authenticatedAdmin ?
          (<Component 
            loggingIn={loggingIn} 
            authenticatedAdmin={authenticatedAdmin} 
            {...rest} 
            {...props} <--- match, location are here
          />) :
          (<Redirect to="/login" />);
        }}
      />
    );
    

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 1970-01-01
      • 2018-12-11
      • 1970-01-01
      • 2016-07-31
      • 2020-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多