-
I have a question about persisting Here's an example: const [search] = useQueryState('search', { defaultValue: 'test' }); I would like this to automatically reflect the default value in the URL on mount, From the documentation, I noticed this note:
I’m curious about what was meant by "unless you set it explicitly". |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
This sounds like the description of a side-effect (updating the URL after mounting), so a good old useEffect might be in order: const defaultValue = 'test'
const [state, setState] = useQueryState('search', parseAsString.withDefault(defaultValue).withOptions({ clearOnDefault: false })
useEffect(() => {
if (state !== defaultValue) {
return
}
setState(defaultValue)
}, []) We can probably get away with an empty dependency array here if |
Beta Was this translation helpful? Give feedback.
This sounds like the description of a side-effect (updating the URL after mounting), so a good old useEffect might be in order:
We can probably get away with an empty dependency array here if
defaultValue
is stable (eg: defined at the top level), as we're only interested in the first state on mount, and not to react to updates.