Skip to content

Commit 82c10ae

Browse files
committed
doc: Add caveat about multiple parsers on the same key
1 parent b7e371d commit 82c10ae

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

packages/docs/content/docs/troubleshooting.mdx

+29
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,32 @@ Because the Next.js **pages router** is not available in an SSR context, this
1313
hook will always return `null` (or the default value if supplied) on SSR/SSG.
1414

1515
This limitation doesn't apply to the app router.
16+
17+
## Caveats
18+
19+
### Different parsers on the same key
20+
21+
Hooks are synced together on a per-key bassis, so if you use different parsers
22+
on the same key, the last state update will be propagated to all other hooks
23+
using that key. It can lead to unexpected states like this:
24+
25+
```ts
26+
const [int] = useQueryState('foo', parseAsInteger)
27+
const [float, setFloat] = useQueryState('foo', parseAsFloat)
28+
29+
setFloat(1.234)
30+
31+
// `int` is now 1.234, instead of 1
32+
```
33+
34+
We recommend you abstract a key/parser pair into a dedicated hook to avoid this,
35+
and derive any desired state from the value:
36+
37+
```ts
38+
function useIntFloat() {
39+
const [float, setFloat] = useQueryState('foo', parseAsFloat)
40+
const int = Math.floor(float)
41+
return [{int, float}, setFloat] as const
42+
}
43+
```
44+

0 commit comments

Comments
 (0)