Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get URL ID in react

Tags:

reactjs

Hey I have a question how can we catch the id from url

 onProductClick = item => {
    this.props.history.push(`/product/${item.id}`);
  };

How can we assign this coming id from url to variable in react

This is the place i've planned to catch the id valuer

class ProductView extends React.Component {
 componentDidMount() {
let someVar = this.props.location.pathname.split('/').pop()
console.log('router val ', someVar);}
like image 884
Fed Avatar asked Jan 20 '26 22:01

Fed


1 Answers

It seems as if you are using react-router, the route in which you want to fetch the id should be added while declaring routes like this. > path="/product/:id" This should be done where you are mentioning your routes and routing a particular component.

Inside the routed component who will find the id in the props mostly with this value. this.props.match.params.id

This may differ depending upon the routing technique but you can log your props and fetch the value with the proper key.

like image 90
Raj Saraogi Avatar answered Jan 22 '26 12:01

Raj Saraogi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!