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.
Just spent some time debugging and issue where I'm writing a test like:
And wondering why it didn't seem to be respecting the existing localStorage values.
Turns out because I hadn't provided a
serializerproperty in the options object, the defaultparseJSONserialization is throws, and then the default value is used.It would be to be aware that this is happening and either: