-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
[v6] feature request: allow searchParams to be passed to useNavigate's options to navigate to a URL with a query string #7743
Comments
I believe you can provide search params today navigate({
pathname: 'listing',
search: '?foo=bar'
}); You can create search Pram using URLSearchParams https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/URLSearchParams#examples |
That's close, but it still requires prepending navigate({
pathname: "listing",
search: `?${createSearchParams({
foo: "bar"
}).toString()}`
}) It looks like history wants to just keep the string: remix-run/history#478. Regarding the I think it'd be trivial for react-router to check if an object was passed through and convert it to a query string automatically, but I guess it's fairly trivial to do it manually too. |
@caseyjhol, the navigate({
pathname: "listing",
search: '?' + createSearchParams({ foo: "bar" })
}) Which i think is not that bad 🙂 I would also like to have a simple way to pass search params as an object. The immediate problem i see with adding this to // should this navigate to /listing?foo=bar, /listing?baz=qux, or /listing?foo=bar&baz=qux ?
navigate({
pathname: 'listing',
search: '?foo=bar',
searchParams: { baz: 'qux' }
});
// should this navigate to /listing?foo=bar or to /listing?foo=42, or even /listing?foo=bar&foo=42 ?
navigate({
pathname: 'listing',
search: '?foo=bar',
searchParams: { foo: 42 }
}); None of those questions is obvious to me. I think react-router made a good decision of staying away from query-string parsing in general (except in providing a hook for a URLSearchParams object, which is a browser builtin). BTW, i think your solution of implementing a custom |
Oh good catch. That definitely cleans it up a bit. I don't think we need a separate |
I think being able to set // navigate to /listing?foo=bar
navigate({
pathname: "listing",
search: "?foo=bar",
});
// also navigate to /listing?foo=bar
navigate({
pathname: "listing",
search: {
foo: "bar",
},
}); This would be really useful when merging the parameters for the "new" page with any existing parameters. const currentSearch = useSearch();
const currentPage = currentSearch.page ?? 1;
navigate({
pathname: "listing",
search: {
...currentSearch,
page: currentPage + 1
},
}); It could also handle arrays of parameters, for example: // navigate to /listing?fruit=apple&fruit=banana
navigate({
pathname: "listing",
search: {
fruit: ["apple", "banana"],
},
}); |
Oh, i hadn't thought about that; you are completely right! It would be super convenient in some cases (e.g., dynamic values), and also wouldn't introduce any ambiguity problem like the ones i described 👍 |
Just a note that you no longer need to prepend navigate({
pathname: "listing",
search: "foo=bar",
}); Overloads add complexity which add code, and we're always doing what we can to weigh DX against keeping the library as lean as possible. I definitely see the utility here, but I believe it should probably be handled in user code. My suggestion would be creating your own import { useNavigate as useNavigate_ } from "react-router-dom";
import { parsePath } from "history";
export function useNavigate() {
let navigate = useNavigate_();
return React.useCallback((to, { searchParams, ...options } = {}) => {
let search = typeof searchParams === "object"
? new URLSearchParams(searchParams).toString()
: undefined;
to = typeof to === "string" ? parsePath(to) : to;
return navigate({ search, ...to }, options);
}, [navigate]);
} |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I'd like to be able to do something like:
See https://stackoverflow.com/questions/65800658/react-router-v6-navigate-to-a-url-with-searchparams for more information.
The text was updated successfully, but these errors were encountered: