Replies: 7 comments 2 replies
-
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 |
Beta Was this translation helpful? Give feedback.
-
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. |
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
Oh good catch. That definitely cleans it up a bit. I don't think we need a separate |
Beta Was this translation helpful? Give feedback.
-
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"],
},
}); |
Beta Was this translation helpful? Give feedback.
-
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 👍 |
Beta Was this translation helpful? Give feedback.
-
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]);
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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.
Beta Was this translation helpful? Give feedback.
All reactions