Skip to content

Provide more of a warning if the the parsing/serializing is failing.  #73

@dwjohnston

Description

@dwjohnston

Just spent some time debugging and issue where I'm writing a test like:

function TheComponent() {
    const [value, setValue] = useLocalStorageState("storage-key"); 
    
    return <div>{value}</div>
}


//test 

localStorage.set("storage-key", "2024-05-01T12:00:00Z"); 
render(<TheComponent/>) 

And wondering why it didn't seem to be respecting the existing localStorage values.

Turns out because I hadn't provided a serializer property in the options object, the default parseJSON serialization is throws, and then the default value is used.

It would be to be aware that this is happening and either:

  • (easy) - console.warn to tell the developer that serialization has failed
  • (breaking change) - Throw an error if serialization fails. Provide an 'opt out of errors' option if the user wants to supress this option.
  • (non-breaking middleground) - provide an opt in - 'throw if serialization fails' option.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions