usehooks-ts
usehooks-ts copied to clipboard
Error parsing data that are stored manually or by other means
When you set a local-storage item manually or by other means which is not using the hook, it throws an error.
I am using a custom script to store theme
info which is just a string, so it does not need to be stringified. But when I am accessing the stored item using the hook it is sending undefined
.
Local storage
theme
: dark
Relevant log output
parsing error on {value: 'dark'}
Possible cause
This is probably happening because of the psrseJson
function
function parseJSON<T>(value: string | null): T | undefined {
try {
return value === 'undefined' ? undefined : JSON.parse(value ?? '')
} catch {
console.log('parsing error on', { value })
return undefined
}
}