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

Different behavior of useLocation depends on base property #498

Open
makzimko opened this issue Dec 12, 2024 · 1 comment
Open

Different behavior of useLocation depends on base property #498

makzimko opened this issue Dec 12, 2024 · 1 comment

Comments

@makzimko
Copy link

makzimko commented Dec 12, 2024

Usign wouter v3 (3.3.5 for now) and watching different behavior of useLocation hook.
Previously I was not using base prop for Router and using useLocation to update search query in URL like that:

const [_, setLocation] = useLocation();
const setCategory = (id: string) => {
  setLocation(`?category=${id}`)
}

So starting from /games page after execute of setCategory('123') I was navigated to /games?category=123.

Now I'm trying to add language into URL using base prop for Router and have different behavior: when using base=en and starting from URL /en/games I navigated to /en?category=123.
Please tell me is it expected behavior or it needs to be fixed somehow?
Thank you in advance!

@molefrog
Copy link
Owner

In wouter terms, ?category=1 isn't a valid path as it doesn't start with a slash so this isn't guaranteed to work properly. I see two workarounds here:

  1. Provide the current path explicitly setLocation('${location}?category=${id}')
  2. Or use location.search = "..." to directly update the search string

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

No branches or pull requests

2 participants