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

Calling setQuery empties a location hash #229

Open
RomanTokar opened this issue Aug 17, 2022 · 4 comments
Open

Calling setQuery empties a location hash #229

RomanTokar opened this issue Aug 17, 2022 · 4 comments

Comments

@RomanTokar
Copy link

RomanTokar commented Aug 17, 2022

When we call setQuery our existing location hash will be emptied.
https://codesandbox.io/s/use-query-params-issue-2eh8n7?file=/src/App.js
I tried to change, for instance, ReactRouter5Adapter to this and it likely solved this problem.

const ReactRouter5Adapter = ({ children }) => {
  const history = useHistory();

  const adapter = {
    replace(location) {
      history.replace({ ...history.location, search: location.search }, location.state);
    },
    push(location) {
      history.push({ ...history.location, search: location.search }, location.state);
    },
    get location() {
      return history.location;
    },
  };

  return children(adapter);
};
@RomanTokar RomanTokar changed the title Changing query params empties a hash Calling setQuery empties a location hash Aug 17, 2022
@pbeshai
Copy link
Owner

pbeshai commented Aug 17, 2022

Yeah that's very true. I made this change based on the way react-router works with search params (see this comment for their explanation on removing the hash). I don't necessarily agree with their reasoning, but I don't really use hashes myself very much to have a strong opinion. I'm unsure whether or not the provided adapter should do as you show above or not.

@RomanTokar
Copy link
Author

RomanTokar commented Aug 17, 2022

@pbeshai I guess I also don't agree.
Here is an codesandbox with applied above adapter.

@andymerskin
Copy link

andymerskin commented Jan 25, 2024

@roman

Great fix, thanks for sharing! Totally agree that the hash shouldn't get nuked when search param changes happen. A lot of applications have tab components or other interactive UI elements that use URL hashes to target them, like going directly to a route with a specific tab selected when the page loads.

For others needing to use this fix in their project locally:

  1. Modify ./node_modules/use-query-params/adapters/react-router-5/index.js in your project with the above changes

  2. Use patch-package link to store a diff of the changes. This tells your package manager to install your local patch whenever you install dependencies for your project, and helps document the changes you've made. Useful for bugfixes like these when your favorite package has lost maintainers or isn't moving on fixes.

  3. Import the JS file directly, rather than the built index.cjs.js file by default:

    import { ReactRouter5Adapter } from "use-query-params/adapters/react-router-5/index";

    Your project may need allowJs in your TypeScript config. Change your configuration accordingly. The import statement may vary depending on your bundling and build setup, but you just need to import the unbuilt JS file from source that you've modified — or jump through the hoops of building this package locally and using it.

@vincerubinetti
Copy link

Having the same issue with ReactRouter6Adapter. Surprised this isn't a more common complaint and urgent fix. I feel like this library should try to be as non-destructive to the URL as possible.

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

4 participants