usehooks-ts icon indicating copy to clipboard operation
usehooks-ts copied to clipboard

Error parsing data that are stored manually or by other means

Open oviirup opened this issue 2 years ago • 0 comments

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
  }
}

oviirup avatar Jul 24 '22 18:07 oviirup