Guidance needed when using useStorage with a null default value #1640
Bobakanoosh
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I run into the scenario where I want to save an object in local storage, but only if it has a value. Because my
initialValue
isnull
, the serializer thatuseStorage
chooses is:This of course makes sense, but once I put in an object as the value, it ends up storing
[object Object]
.I could just have an initial value of
{}
, causing it to use the serializer for objects.This has two problems:
x === null
, and instead have to use custom logic to check if an object is empty. This is not very intuitive because Typescript will indicate that the object can never be null, fooling the consumer of the variable to think that the stored value has no empty state.Solutions:
In the meantime I've been using custom serializes that handle the null case, which is less than ideal since now I have to remake all of the serialize code that
useStorage
does.Maybe there's a more simple answer that I'm missing, or maybe I'm being overly picky when it comes to the problems I pointed out with
{}
as the empty state. Any guidance/ideas would be helpful.Beta Was this translation helpful? Give feedback.
All reactions