Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
react-dom-router Link component doesnt work as expected

I have a react app with two pages--- the home and trip page. The trip page is rendered with the tripID passed in through the url. My app.js looks like :

function App() {
  return (<>
    <ThemeProvider theme={theme}>
      <Router>
        <Routes>
          <Route exact path='/' element={<Home />} />
          <Route path='/trip/:tripId' element={<TripComponent />} />
        </Routes>
      </Router>
    </ThemeProvider>
  </>
  );
}

I have a global navbar with a menu of different tripIds as many Link to navigate to the TripComponent. When i am at the "/" path, and I navigate to "/trip/tripA", i have no problems. But when i am at "/trip/tripA" , and i want to navigate to "/trip/tripB", it doesnt work. I can see the url changing accordingly, but my TripComponent doesnt rerender with tripB's data. the code for my menu in my navbar looks like:

ReactDOM.createPortal(<>
    <CustomModal setOpen={setShowTripList} title={"Saved Trips"}>
            <List>
                {trips && trips.length > 0 &&
                    trips.map((trip, index) => {
                        return (
                            <Link to={`/trip/${trip._id}`} >
                                <ListItemButton>
                                    <ListItemText id={trip._id} primary={trip.title} />
                                </ListItemButton>
                            </Link>
                        )
                    })
                }
            </List>
    </CustomModal>
</>
    , document.getElementById("portal"))

I am confused as to why this is happening. When i press the Link to navigate to another URL, shouldn't it unmount and remount?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

When the tripId route path parameter updates the routed element TripComponent isn't remounted, it is only rerendered. If there is some logic that depends on the tripId path parameter then TripComponent needs to "listen" for changes to that value. This is the same thing that would need to happen if a prop value changed.

Use a useEffect hook with a dependency on the tripId path parameter to issue side-effects based on the current value.

Example:

import { useParams } from 'react-router-dom';
...

const TripComponent = () => {
  const { tripId } = useParams();

  useEffect(() => {
    // initial render or tripId updated
    // do something with current tripId value
  }, [tripId]);

  ...
};
about 4 years ago · Santiago Trujillo Report

0

I think the @Drew's answer is perfect. But I'd like to put something additionally.

I suggest you to use useMemo hook.

...
const trip = useMemo(() => {
    // something you want
}, [tripId])
...
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!