Possible race condition with `usePath`
See original GitHub issueHello!
While working on a project @ my day job I came across what looks to be a race condition with usePath
and navigate
.
I’ve created a code sandbox that reliably replicates the issue: https://codesandbox.io/s/ecstatic-silence-16zr4
Steps to reproduce
- Navigate to
/about
. - Reload the page so that we ‘start’ on
/about
. - Open up your browser console so you can see the logs.
- Press 1 on the keyboard.
What should happen: Navigates to home page, one console log from Home
saying home path: /
What actually happens: Navigates to home page, but we get two console logs. One from the /about
component, and one from the /
root component.
From what I can gather, using navigate
seems to update the value returned by usePath
before the navigation is complete - thus giving components we are navigating away from the chance to run code based off the changed path value. For example, in a useEffect
:
useEffect(() => {
setTabUrl(path);
}, [path, setTabUrl]);
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (12 by maintainers)
Top Results From Across the Web
Race condition when fetching request.url in Node / Express
Fixed this by making the request object available to my templates in a middleware function (beforehand I set it as a global on...
Read more >File Path Race Condition & How To Prevent It - bin 0x31
In this video we look at a typical race condition involving file paths. We learn about the renameat syscall to exploit it and...
Read more >Kubernetes dataclient race condition · Issue #304 · zalando/skipper ...
I am testing skipper with k8s and I am having weird behaviour with some requests that are ending arbitrarily in different endpoints.
Read more >Looking for a performant way to prevent a race condition.
Is there any cleaner way to prevent the race condition? Or is there a better way to use the locking mechanism? Any help...
Read more >Race conditions in React and beyond. A race condition guard ...
Therefore, they are racing to access it. Our code does not need to utilize multiple threads to have a race condition. A similar...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@kyeotic I just performed a test with the beta version and that fixed the (specific) issue we were facing without any change in our code base! 🥳
I’ll send a pull request for the library upgrade and we will dog food it for a while to make sure there are no routing regressions. We’ll let you know!
Thanks again!
@fpyir
I’ve released a beta under
1.4.6-0
. Install withThis reworks the internals of
useRoutes
so that it tracks the path differently. Unit testing shows that code inuseEffect
hooks do not receive the wrong path. Im releasing this as a beta because none of the apps I manage have encountered this bug and I am unsure of the total impact of this change.I will leave it as a beta for two weeks, or until you confirm it fixes your issue without introducing any other bugs. Whichever comes first.