mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-10-02 16:20:36 -05:00
set search params without the whole layout reloading
This commit is contained in:
18
lib/setSearchParams.ts
Normal file
18
lib/setSearchParams.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
export const setSearchParams = (key: string, value: string | undefined) => {
|
||||
const url = new URL(window.location.href);
|
||||
const params = new URLSearchParams(url.search);
|
||||
|
||||
if (!value) {
|
||||
params.delete(key);
|
||||
} else {
|
||||
params.set(key, value); // encodeURIComponent(value)
|
||||
}
|
||||
|
||||
history.replaceState(
|
||||
{},
|
||||
"",
|
||||
`${window.location.pathname}${
|
||||
Array.from(params.entries()).length ? "?" : ""
|
||||
}${params.toString()}`
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user