This might be an unintended use of the hook, but it's a use nonetheless.
I have a component Filter
which the user interacts with through input
controls in order to specify a filter
, which should then be used for searching.
My goal is to have the filter
saved to the url, so that the user may then link the search page, with the filter
they specified. I figured that useQueryParams
would cover this use-case as a kind of "drop-in replacement" for useState
, which just so happens to load/save the state to the query string.
However, for parameters of the type StringParam
, the useQueryParams
hook seems to default "empty" values to undefined
- seemingly in order to remove them from the query string - likely due to the parameter encoder.
This means that any state variables set by useQueryParams
(or useQueryParam
), will cause input
controls to switch from being uncontrolled to controlled when the user enters an initial value, or vice-versa when the user deletes the value from the control, causing this warning:
Warning: A component is changing an uncontrolled input of type undefined to be controlled. Input elements should not switch from uncontrolled to controlled (or vice versa). Decide between using a controlled or uncontrolled input element for the lifetime of the component. More info: https://fb.me/react-controlled-components
in input (at src/index.js:32)
in Filter (at src/index.js:55)
in Route (at src/index.js:54)
in Route (created by QueryParamProvider)
in QueryParamProvider (at src/index.js:53)
in Router (created by BrowserRouter)
in BrowserRouter (at src/index.js:52)
in App (at src/index.js:63)
Now, I've seen #31, and the proposed solution by @albinekb seems workable for useQueryParam
, but what about useQueryParams
?
I'm not proposing adding default values for the query parameters, but I'd be happy to see some kind of solution to this problem. My simple idea of computing a displayFilter
doesn't cut it.
Demo (with my failed attempt at solving the issue): https://codesandbox.io/s/bitter-moon-81gzo