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
)
newQueryStateValue = null
}
// when new value is equal to default, we call setQueryState with a null value to reset query string
// arrays have to be compared json stringified, other values can compared by value
if (Array.isArray(defaultValue) && sameAsJsonString(newValue, defaultValue)) {
} else if (newValue === defaultValue) {
newQueryStateValue = null
}
setQueryState({ [itemName]: newQueryStateValue }, opts)
},
[defaultValue, itemName, setQueryState]
)
// fallback to default value
let value = defaultValue
const queryStateItem = queryState[itemName]
let queryStateValue = null
if (queryStateItem || queryStateItem === '') {
queryStateValue = parseQueryStateValue(queryStateItem, defaultValue)
}
if (queryStateValue !== null && typeof queryStateValue === typeof defaultValue) {
value = queryStateValue as any
}
return [value, setQueryStateItem]
}