I have a react component with the following function
const handleNavigate = (clientId) => {
console.log(clientId)
navigate(`/dashboard/clients/${clientId}`)
}
The console.log() is showing the ID I want to append to use in the navigate function.
AND
The URL in the browser is updating.
But the page does not change.
This works to navigate from /dashboard/clients
to /dashboard/clients/foo
but it does not work to navigate from /dashboard/clients/foo
to /dashboard/clients/bar
The clientId is passed into the card like so...
const CompanyCard = (props) => {
const { client, showWatchlist, removeDisabled, showRemove, removeType } = props
...
}
then in the card
<CardActionArea
onClick={() => handleNavigate(client._id)}
...
Any ideas?
Thanks
After reading up from @redapollos suggestion I tried Outlet
and the
useRoutes methods... neither worked.
import { useRoutes } from 'react-router-dom'
// then in the routes...
{ path: 'clientdetail/:id', element: <ClientDetail /> },
{ path: 'clientdetail/', element: <ClientDetail /> },
This might be due to using the useRoutes
hook but I am still working on it.
Another question here on SO that might get an answer sooner - https://stackoverflow.com/a/70009104/13078911
navigate('/url')
navigate(0)
After replacing the url, manually calling navigate(0)
will refresh the page automatically!
This will work in a situation like
navigate from /same_path/foo
to /same_path/bar
.
For a normal situation like navigate from /home
to /same_path/bar
, navigate(0)
will cause page to refresh even after page has finished rendering. And to prevent that, you can take a look at this question.
useNavigate docs
How do I reload a page with react-router?
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