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]: window.location.search is stale value and not synchronized with useLocation().search #10289

Closed
mrdulin opened this issue Mar 30, 2023 · 3 comments
Labels

Comments

@mrdulin
Copy link

mrdulin commented Mar 30, 2023

What version of React Router are you using?

6.9.0

Steps to Reproduce

React version use loader issue happen
16.14.0 Y Y
16.14.0 N N
18.2.0 Y N
18.2.0 N N

React 16.14.0 reproducible example:
codesandbox

React 18.2.0 working example:
stackblitz

Expected Behavior

The value of window.location.search should be synchronized with useLocation().search, that is ?status=2.

Actual Behavior

Using the navigate() to replace the url search parameter.

console.log("useLocation().search: ", location.search);
console.log("window.location.search: ", window.location.search);

When the route is /list?status=1, output:

useLocation().search:  ?status=1 
window.location.search:  ?status=1 
useLocation().search:  ?status=1 
window.location.search:  ?status=1

After calling navigate('/list?status=2'), output:

useLocation().search:  ?status=1 
window.location.search:  ?status=1 
useLocation().search:  ?status=1 
window.location.search:  ?status=1 
useLocation().search:  ?status=2 
window.location.search:  ?status=1 
useLocation().search:  ?status=2 
window.location.search:  ?status=1

As you can see, the value of window.location.search is always stale value:?status=1

@mrdulin mrdulin added the bug label Mar 30, 2023
@timdorr
Copy link
Member

timdorr commented Mar 30, 2023

This isn't a bug with the library. If React is flushing things to the DOM too quickly, that seems to be an issue on their end. Nonetheless, you are using an older version of React so bugs should be expected.

@gaearon
Copy link
Contributor

gaearon commented Mar 31, 2023

If I understand correctly, the substance of the complaint is

the value of window.location.search is always stale value:?status=1

In other words, after calling navigate('/list?status=2') the ?status in the URL bar (as given by window.location) is still 1. This seems to imply that it's the URL navigation that has not been flushed. I assume it's controlled by the router (unless React is forgetting to call some Effect?). I don't see how React flushing to the DOM is relevant here yet.

@matijs
Copy link

matijs commented Apr 1, 2023

@mrdulin have you looked into getting the value of status using React Router's useSearchParams?

Stackblitz example

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

4 participants