Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: useParams hook regularly makes new params empty object #9011

Closed
JasonKleban opened this issue Jun 24, 2022 · 1 comment
Closed

[Bug]: useParams hook regularly makes new params empty object #9011

JasonKleban opened this issue Jun 24, 2022 · 1 comment
Labels

Comments

@JasonKleban
Copy link

JasonKleban commented Jun 24, 2022

What version of React Router are you using?

6

Steps to Reproduce

return routeMatch ? (routeMatch.params as any) : {};

Expected Behavior

I think this should be some constant emptyObj reuse instead of made anew because it causes rerenders.

I'm also seeing truthy a routeMatch give an identical object over and over but I was under the impression that this was fixed in v6 with #7059 and #8431.

Actual Behavior

returns a new {} every time.

or when routeMatch is truthy, unnecessary rerenders with something like { "*": "" } as the return value.

JasonKleban added a commit to JasonKleban/react-router that referenced this issue Jun 24, 2022
Addresses remix-run#9011 useParams()'s new `{}` return
@timdorr
Copy link
Member

timdorr commented Jun 26, 2022

You shouldn't be using the entire result of useParams in your dependencies for React hooks. If you navigate and don't change the params values, you'll have the same problem as the object will still be recreated. Instead, you should check specific params that you care about. That will ensure you don't re-render when it's not needed, regardless of what the state of params is.

@timdorr timdorr closed this as completed Jun 26, 2022
brophdawg11 pushed a commit that referenced this issue Mar 27, 2024
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants