I am using useSearchParams
(react-router-dom@6
). When I add a new query parameter it deletes the previous ones. How can I prevent this?
When I try this
const filterFunc = (valu) => {
setSearchParams({ star: valu });
}
this is happening: http://localhost:3000/products/search?star=1
my expectation: http://localhost:3000/products/search?q=car&star=1
I adapted this answer to my own code, but I don't think it's optimal.
code:
const [searchParams, setSearchParams] = useSearchParams();
const params = {};
searchParams.forEach((value, key) => {
params[key] = value;
});
const filterFunc = (star) => {
params["star"] = star;
setSearchParams(params);
}