Replies: 1 comment 3 replies
-
The problem is you're logging on render, and because useParams reads from the router single context, any internal state change in the route will cause a new render. When you navigate from This is usually not an issue, if you use an effect that depends on the param value you can grab exactly that param, that way unless the string change the effect won't run again, e.g. let id = useParams().id
useEffect(() => {
console.log({ id })
}, [id]) That effect will only run after the |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Here's a demo on CSB:
https://codesandbox.io/p/devbox/remix-useparams-dk8n2q?file=%2Fapp%2Froutes%2Fitems.%24id.tsx%3A4%2C5-4%2C31
When clicking on
/items/aa
from/items/bb
,const params = useParams()
first returns{"id": "aa"}
and then{"id": "bb"}
. I believe this behavior is not ideal, as it will cause the sub-page to render/items/aa
again. In some cases, this may lead to problems such as unexpected useEffect executions.However,
useLocation()
returns only once. I have to manually parse the parameters fromuseLocation
to avoid the problems associated withuseParams
.Beta Was this translation helpful? Give feedback.
All reactions