Bad understanding of useQueryStates #674
-
I think there is some things that I don't understand with I have a list of many filters that I wanted to show in the url with So first, I passed an empty object in and I reallized that when I change one filter, my state was not updated and so the url. 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). The thing is I don't want to list all my filters since I have so many, included some filters generated dynamically by the result of an API call. I saw this post that suggest to use a parser, but it seams to work only with params that already are in the url. So my question is, is there a way to work like I want ? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
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 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(parsers) |
Beta Was this translation helpful? Give feedback.
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: