I already used setSearchParams hook in Parent component , which set query depending on input from input JSX element. Now , I need to implement second search param = sortBy , but not to erase already or might be existing query search param I need to perform each time If() chech.
And also I will need if() checks in parent elements now for sortBy param. And potentially I might introduce few more search params in different React components - this will clutter the code.
Is there a better approach to handle multiple search params scattered through your project with React Router 6?
import { useSearchParams } from 'react-router-dom';
const titles = ['Name', 'Sex', 'Born', 'Died', 'Father', 'Mother', 'Slug'];
export const TableHead = () => {
const [searchParams, setSearchParams] = useSearchParams();
const query = searchParams.get('query');
return (
<tr className="person">
{
titles.map(el => (
<th
key={el}
onClick={() => {
if (query) {
setSearchParams({ query, sortBy: el.toLowerCase() });
} else {
setSearchParams({ sortBy: el.toLowerCase() });
}
}}
>
{el}
</th>
))
}
</tr>
);
};