Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(newValue, opts) => {
// stringify the given value (or array of strings)
let newQueryStateValue = toQueryStateValue(newValue)
// warn when value type is not supported (do not warn when null was passed explicitly)
if (
(newQueryStateValue === null && newValue !== newQueryStateValue) ||
!(
newQueryStateValue !== null &&
typeof parseQueryStateValue(newQueryStateValue, defaultValue) === typeof defaultValue
)
) {
console.warn(
'value of ' +
JSON.stringify(newValue) +
' is not supported. "' +
itemName +
'" will reset to default value',
defaultValue
export default function useQueryState(
itemName: string,
defaultValue: T,
queryStateOpts: QueryStateOpts = {}
): [T, SetQueryStateItemFn] {
// defaultValue is not allowed to be changed after init
;[defaultValue] = useState(defaultValue)
const defaultQueryStateValue = toQueryStateValue(defaultValue)
const defaultQueryState = useMemo(() => {
return defaultQueryStateValue
? {
[itemName]: defaultQueryStateValue,
}
: {}
}, [itemName, defaultQueryStateValue])
if (defaultQueryStateValue === null) {
throw new Error('unsupported defaultValue')
}
const [queryState, setQueryState] = useQueryStateObj(defaultQueryState, queryStateOpts)
const setQueryStateItem: SetQueryStateItemFn = useCallback(
(newValue, opts) => {
// stringify the given value (or array of strings)