Skip to content

Bad understanding of useQueryStates #674

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

You must be logged in to vote

I suppose it use the object passed as a reference and update the url only with the properties that match between them (none in this case).

Your intuition is correct: the object passed in is the reference for which keys are managed by the hook.

useQueryStates was designed to accept arbitrary keys, so if they are coming from an external source, you could craft a { [key: string]: parseAsString } object to pass it:

const filterKeys = ['foo', 'bar', 'egg'] // Suppose this is returned from an API call
const parsers = useMemo(
  () => Object.fromEntries(filterKeys.map((filterKey) => [filterKey, parseAsString])),
  [filterKeys.join(',')]
)

const [searchParams, setSearchParams] = useQueryStates(p…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@abhion
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
3 participants