Skip to content

Persisting defaultValue in the URL on Mount #842

Answered by franky47
isBatak asked this question in Q&A
Discussion options

You must be logged in to vote

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 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.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@isBatak
Comment options

@franky47
Comment options

Answer selected by franky47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants