26

I have a route set up to render a component:

<Route exact path="/page/:id" location={this.props.location} key={this.props.location.key} render={({ location }) => (
    <PageStart key={this.props.location.key} />
)} />

Then inside that component (PageStart) I have:

this.props.match.params.id

But it throws an error:

Cannot read property 'params' of undefined

Passing props when simply calling component={} seems to work fine but not in a render function. Why?

B--rian
  • 5,578
  • 10
  • 38
  • 89
Matt Saunders
  • 4,073
  • 7
  • 33
  • 47

6 Answers6

36

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.

David Schumann
  • 13,380
  • 9
  • 75
  • 96
Mayank Shukla
  • 100,735
  • 18
  • 158
  • 142
7

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} />
    )} 
/>
David Schumann
  • 13,380
  • 9
  • 75
  • 96
ChrisR
  • 3,922
  • 1
  • 14
  • 24
4

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!

jenkizenki
  • 741
  • 6
  • 14
3

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} />
  )}
/>
David Schumann
  • 13,380
  • 9
  • 75
  • 96
goto-bus-stop
  • 11,655
  • 2
  • 24
  • 31
1

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.

Rahul Shakya
  • 1,269
  • 15
  • 15
0

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

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