Skip to content

Commit

Permalink
Add custom function to commit search params using API that also works…
Browse files Browse the repository at this point in the history
… for NextJS
  • Loading branch information
vgeorge committed Nov 21, 2024
1 parent 903807f commit 34d878f
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@ export function useFiltersWithURLAtom(): UseFiltersWithQueryResult {
}

export function useFiltersWithQS(): UseFiltersWithQueryResult {
const useQsState = useQsStateCreator();
const useQsState = useQsStateCreator({
commit: ({ search }) => {
if (typeof window !== 'undefined') {
const currentUrl = new URL(window.location.href);
const searchParams = search.startsWith('?') ? search.slice(1) : search;
currentUrl.search = searchParams;
window.history.pushState({}, '', currentUrl.toString());
} else {
// eslint-disable-next-line no-console
console.log(
'useFiltersWithQS: window is undefined, query string will update.'
);
}
}
});

const [search, setSearch] = useQsState.memo(
{
Expand Down

0 comments on commit 34d878f

Please sign in to comment.