URL search parameters gets replaced - Remix Run #1788
-
I'm working on a search UI where I have quite a few filters which I want as URL parameters when someone selects/checks the options. I've used the technique as advised on the Remix.run docs to come up with multiple forms within the filters. I've posted the question on StackOverflow. can someone help? :D |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I've ran into this a few times, basically each form just replaces the url params with it's own search params. To keep both you need to add hidden inputs to each form with all the search params you want to keep: // In the filters
<input type="hidden" name="search" value={params.get("search")} /> So when you submit this form it will also "keep" the "search" search param, by also submitting it. |
Beta Was this translation helpful? Give feedback.
-
@coderkk1992 I have two questions for you, for my curiosity:
const Search = () => {
const data = useLoaderData();
const [searchParams] = useSearchParams();
return (
<>
<h2 className="mb-2 w-full">Recherchez un sujet</h2>
<Form className="flex w-full flex-col gap-3">
<input type="search" name="search" defaultValue={searchParams.get("search") || ""} />
<input type="search" name="category" defaultValue={searchParams.get("category") || ""} />
<button type="submit">Rechercher</button>
</Form>
<pre>{JSON.stringify(data, null, 2)}</pre>
</>
);
};
where did you find this technique ? |
Beta Was this translation helpful? Give feedback.
I've ran into this a few times, basically each form just replaces the url params with it's own search params. To keep both you need to add hidden inputs to each form with all the search params you want to keep:
So when you submit this form it will also "keep" the "search" search param, by also submitting it.