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]: useSearchParams hook removes path name from url #9045

Closed
uditjaitly opened this issue Jul 9, 2022 · 5 comments · Fixed by #9048
Closed

[Bug]: useSearchParams hook removes path name from url #9045

uditjaitly opened this issue Jul 9, 2022 · 5 comments · Fixed by #9048
Labels

Comments

@uditjaitly
Copy link

What version of React Router are you using?

6.4.0-pre.8

Steps to Reproduce

Sandbox demonstrating the unexpected behavior could be found below

https://codesandbox.io/s/boring-banach-ri3tiv?file=/src/App.js

The behavior is erratic only if I have a basename="assets" as a BrowserRouter Prop. If I remove the basename prop, normal functionality is restored

Expected Behavior

When I do setSearchParams( {foo:'bar'} ) when on url path basename/id1, the url should get changed to basename/id1?foo=bar

Actual Behavior

When I do setSearchParams( {foo:'bar'} ) when on url path basename/id1, the url is changed to basename?foo=bar

For eg, in the sandbox:
https://codesandbox.io/s/boring-banach-ri3tiv?file=/src/App.js

Sandbox Steps:

  • When on url /assets Click on the button 'Click me!'
  • That should take you to /assets/id1, now click on the button 'Button One'

Notice how the url changes to assets?buttonone=test and not assets/id1?buttonone=test

@uditjaitly uditjaitly added the bug label Jul 9, 2022
@uditjaitly uditjaitly changed the title [Bug]: [Bug]: setSearchParams removes path name from url Jul 9, 2022
@brophdawg11 brophdawg11 self-assigned this Jul 10, 2022
@uditjaitly uditjaitly changed the title [Bug]: setSearchParams removes path name from url [Bug]: useSearchParams hook removes path name from url Jul 10, 2022
@brophdawg11
Copy link
Contributor

Thanks for the reproduction! Looks like this busted with #8861 - digging in now 👍

@brophdawg11
Copy link
Contributor

Fixed in #9048 - should have a 6.4.0-pre.9 release out soon

@jrmyio
Copy link

jrmyio commented Jul 19, 2022

Still have this issue in 6.4.0-pre.9 for a *-segment after navigate() was used to update the *-segment.
Then calling setSearchParams removes the star segment.

@brophdawg11
Copy link
Contributor

@jrmyio would you be able to provide a simple code sandbox reproduction to demonstrate the issue?

@jrmyio
Copy link

jrmyio commented Jul 19, 2022

Just tried to replicate it in a codesandbox but couldn't reproduce. Tomorrow I will try to see what makes my project's context break it.

Edit: Found the reason for my issue but it is not a bug in react-router. In the code I assumed setSearchParams is a stable function, but it is actually not. I guess this is somewhat related to #7634.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants