score:37

Accepted answer

react-router-dom provides some handy hooks. in your case, i'd suggest useparams() (link to docs)

import { useparams } from 'react-router-dom';

function mycomponent {
  let { id } = useparams();

  return (<p>{id}</p>);
}

i'd also probably opt for not using withrouter if you will be using the hooks provided by react router

score:2

martins answer is correct and works for functional components. i had the same problem, however i was using a class component (class company extends component) and martins answer did not work for that case. if you have a class component you can do:

import { withrouter } from 'react-router-dom'

class mycomponent extends react.component {

    componentdidmount() {
        // here you have the id
        const id = this.props.match.params.id;
      }

}

export default withrouter(mycomponent);

Related Query

More Query from same tag