Cannot read property ‘params’ of undefined (React Router 4)

后端 未结 6 602
甜味超标
甜味超标 2020-12-29 04:49

I have a route set up to render a component:



        
相关标签:
6条回答
  • 2020-12-29 05:17

    The error says the match prop is undefined. That's correct, there is no match prop here:

    <PageStart key={this.props.location.key} />
    

    So we need to pass it in. The render function receives a bunch of props from react-router, and all of them need to be passed further down. So, spread them first, and then add your own props:

    <Route
      exact
      path="/page/:id"
      location={this.props.location}
      key={this.props.location.key}
      render={(props) => (
        <PageStart {...props} key={this.props.location.key} />
      )}
    />
    
    0 讨论(0)
  • 2020-12-29 05:20

    try this please. i hope it can solve the issue.

    render = {props => <PageStart {...props} key={this.props.location.key} /> } />
    
    0 讨论(0)
  • 2020-12-29 05:21

    Because you don't pass any match property to PageStart. You give it a key but no match. Try this:

    <Route 
        exact 
        path="/page/:id" 
        location={this.props.location} 
        key={this.props.location.key} 
        render={({ 
            location, 
            match 
        }) => (
            <PageStart key={this.props.location.key} match={match} />
        )} 
    />
    
    0 讨论(0)
  • 2020-12-29 05:25

    please check for the routename provided

    const AuthNavigattor = createStackNavigator(
      {
        logins: {screen: LoginScreen},
        splash: {screen: SplashScreen},
        register: {screen: RegisterScreen},
        forgetPassword: {screen: ForgetPasswordScreen},
        mobileNumber: {screen: MobileNumberScreen},
        codeEnter: {screen: CodeEnterScreen},
        changePassword: {screen: ChangePasswordScreen},
        // dashboard: { screen: Drawer }
      },
      {
        // initialRouteName: "splash",
        headerMode: 'none',
      },
    );
    const ProfileNavigattor = createStackNavigator(
      {
        logins: {screen: LoginScreen},
        splash: {screen: SplashScreen},
        register: {screen: RegisterScreen},
        forgetPassword: {screen: ForgetPasswordScreen},
        mobileNumber: {screen: MobileNumberScreen},
        codeEnter: {screen: CodeEnterScreen},
        changePassword: {screen: ChangePasswordScreen},
        // dashboard: { screen: Drawer }
      },
      {
        // initialRouteName: "splash",
        headerMode: 'none',
      },
    );
    

    here there is repitition of same route name like logins, splash etc which might be the reason for this error. Rename this unique will solve your issue.

    0 讨论(0)
  • 2020-12-29 05:27

    In my case, I found out that it is no longer match in the router props, it is now computedMatch.

    I.E: this.props.computedMatch.params.id

    Hope this helps someone!

    0 讨论(0)
  • 2020-12-29 05:29

    Because with render you are not passing the default props passed by the router into component, like match, history etc.

    When you write this:

    <PageStart key={this.props.location.key} />
    

    It means no props value in PageStart component.

    Write it like this:

    render = {props => <PageStart {...props} key={this.props.location.key} /> } />
    

    Now {...props} will pass all the value into PageStart component.

    0 讨论(0)
提交回复
热议问题