question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

useQueryParams hook returns stale values when going back in history

See original GitHub issue

Hello, it’s me again 👋

So it seems that the latest changes introduced for useLocationChange have caused a problem with other hooks using it (not exactly sure why though).

When using useQueryParams, the URL param values returned when going back in history (web browser back button) do not reflect the current window location parameters, they are behind (stale).

I reverted to raviger 0.5.9 and the problem disappeared.

Would you have any idea what could cause that? If that’s too vague I can work on building a reproduction somewhere, let me know.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
yenoiwesacommented, Nov 6, 2019

After more than an hour trying to debug the issue (and it’s now 11.30pm here in Sydney hehe) I think I have found the reason for the bug.

useRoutes will always be used in a parent component of any component that themselves use useQueryParams or usePath.

useRoutes uses useLocationChange here.

If you comment out the useLocationChange usage in useRoutes, the bug disappears.

This is because whenever the popstate event is triggered, it will be fired on the event listeners in the order they were registered, hence the component using useRoutes will receive it first. That component will re-render, causing the children component using the other two hooks to re-render too. This is happening synchronously, before the children hooks get a chance to receive their own popstate event.

But since the useQueryParams hook does not keep a stable reference to the callback it passes to useLocationChange, the render loop being called on that component will cause a new callback to be passed, and the useEffect to trigger its clean up effect, which will unbind the popstate listener.

So when the event loop has finished the render and goes back to firing the remaining popstate event to the other listeners, the useQueryParams listener has already been unbound, and thus will not be triggered.

Now, what I am not clear about is why going forward in history is fine… And I’ll rather go to bed than investigate that!

1reaction
kyeoticcommented, Nov 6, 2019

Try raviger@rc or raviger@1.0.0-rc.3 and let me know

Read more comments on GitHub >

github_iconTop Results From Across the Web

ReactRouterRoute breaks with React Router v6 #108 - GitHub
I have multiple nested useQueryParams in my app (parent and children will both be modifying different query params simultaneously) and I cannot get...
Read more >
Hooks to persist state in the query string or history.state in React
Introducing: useQueryState() The default value will be returned as the current value, as long as the value was not updated and the query...
Read more >
useQuery | TanStack Query Docs
The time in milliseconds after data is considered stale. This value only applies to the hook it is defined on. If set to...
Read more >
react-router-query-params-hook - npm package - Snyk
First you have to create your actual useQueryParams hook. react-router only returns a string for location.search , though you have to provide ...
Read more >
Be Aware of Stale Closures when Using React Hooks
The stale closures is a pitfall of React hooks when an outdated variable is captured by a closure.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found