Skip to content

Commit 3a5544a

Browse files
author
rtrampox
committed
fix(#34): update how values are synced with the current query string state, so changes outside of nuqs-svelte can also be tracked
1 parent 6c17ed9 commit 3a5544a

3 files changed

Lines changed: 12 additions & 11 deletions

File tree

packages/nuqs-svelte/src/lib/adapters/svelte-kit/adapter.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import type { AdapterInterface, AdapterOptions } from "../types";
77
import NuqsContext from "../NuqsContext.svelte";
88
import { renderQueryString } from "$lib/url-encoding";
9-
import { SvelteURL } from "svelte/reactivity";
9+
import { SvelteURL, SvelteURLSearchParams } from "svelte/reactivity";
1010
1111
type Props = {
1212
children?: Snippet;
@@ -51,8 +51,8 @@
5151
}
5252
},
5353
54-
searchParams: () => new URLSearchParams(url.searchParams),
55-
getSearchParamsSnapshot: () => new URLSearchParams(url.searchParams),
54+
searchParams: () => new SvelteURLSearchParams(page.url.searchParams),
55+
getSearchParamsSnapshot: () => new URLSearchParams(page.url.searchParams),
5656
};
5757
</script>
5858

packages/nuqs-svelte/src/lib/use-query-state.svelte.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { untrack } from "svelte";
21
import { useAdapter } from "./adapters/index.svelte";
32
import { debug } from "./debug";
43
import type { Parser } from "./parsers";
@@ -234,22 +233,20 @@ export function useQueryState<T = string>(
234233
})(),
235234
);
236235

237-
let queryState = $derived<string | null>(initialSearchParams.get(key) ?? null);
236+
// this state should represent the previous query string value
237+
// so that we can compare it with the current value
238+
let queryState = initialSearchParams.get(key) ?? null;
238239

239240
$effect(() => {
240-
// the query state needs to be untracked, as its value is updated when the internal state changes,
241-
// and causes this effect to re-run, causing the internal state to be set to it's older value
242-
const uQueryState = untrack(() => queryState);
243-
244241
const query = initialSearchParams.get(key) ?? null;
245242

246243
// parse the query string before comparing, as these values can be boolean, and any string would return true
247244
const state = query === null ? null : safeParse(parse, query, key);
248-
if (state === uQueryState) {
245+
if (state === queryState) {
249246
debug(
250247
"[nuqs `%s`] syncFromUseSearchParams, no change, prev: %O, new: %O",
251248
key,
252-
uQueryState,
249+
queryState,
253250
state,
254251
);
255252
return;

packages/nuqs-svelte/src/routes/+page.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
Toggle Switch
2929
</button>
3030

31+
<a href="/" class="rounded-md bg-neutral-900 p-2 text-center text-white ring ring-neutral-600"
32+
>Reset</a
33+
>
34+
3135
<p>Loader last updated: {data.time.toLocaleTimeString()}.</p>
3236
<p>Query value from loader: {data.query}</p>
3337
<p>Switch value from loader: {data.switch}</p>

0 commit comments

Comments
 (0)