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);}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With