reactjsreact-routerreact-router-componentreact-routing

How to access value of a routing parameter in react?


I want to have a 'dynamic route' for a section of my app, which was done like this:

<Route path="path(/:id)" component={Component} />

So far this works, but in Component I would like to access the value of id because it changes a bunch of things depending on which it is. How can I do that?


Solution

  • In your component, you would access this via props.

    this.props.params.id
    

    Here is the guide from react-router that goes into more detail as well. https://github.com/reactjs/react-router-tutorial/tree/master/lessons/06-params