Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot render same route with different parameters react router v4

I am implementing a react page with route info/:id. From this page, there are various URLs which are navigated by NavLink.

For example: if current route is localhost:3000/info/1 and there are links localhost:3000/info/2 and localhost:3000/info/3, clicking on those links will load the page I'm currently on i.e: localhost:3000/info/1. While tracking for props changes, url is getting changed briefly but it's again loading the same page. What shall I do to route to same component with different parameters?

Thanks in advance!!!!!!

like image 769
pramesh Avatar asked Sep 06 '18 17:09

pramesh


People also ask

Which props should you use to match exactly the path you gave for routing?

The exact prop is used to define if there is an exactly the requested path.

Which of the following prop allows you to access parameters passed in routing with react router v4?

If you are using react-router v4, you can pass it using the render prop.

Can you have multiple routes react?

Another incredibly powerful thing you can do with React Router is use multiple Routes components at the same time. This can be done as either two separate Routes components or as nested Routes .

Can we use render in route?

The render prop function has access to all the same route props (match, location and history) as the component render prop.


1 Answers

You should track your page updates in componentDidUpdate lifecycle method or useEffect hook.

Class components:

 componentDidUpdate(prevProps) {
   if(prevProps.match.params.id !== this.props.match.params.id){
     // do something
   }
 }

Functional components:

useEffect(() => {
    // do something
}, [props.match.params.id]);
like image 166
Soroush Chehresa Avatar answered Sep 30 '22 05:09

Soroush Chehresa