You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, working with useLocalStorageValue is frustrating as it specifically disables setting a value to null, yet forces null to be a type the value can be. It also forces undefined to be a type value can be. I can understand the reasoning of not wanting to allow null for JSON parse/stringify reasons as mentioned in the docs, but I feel like it needs to be consistent. Since I'm setting my own parse/stringify, I was able to get null to be a valid value, but now it's forcing undefined to be an option, event if I set initializeWithValue to true. So in order for my types to be correct, I have to wrap the hook with a custom hook that coalesces the value to null if undefined. What's the reasoning behind forcing both null and undefined?
undefined comes from the fact that value can be not fetched on init, in case initializeWithValue set to false - it is required for SSR compatibility. if you'll set initializeWithValue option to true - there will be no undefined in state typings.
Deprecation of null comes from Storage value itslef as there is no way (without performing extra-calls to storage) to distinguish null stored as value and null meaning that there is no such key in storage.
About undefined type even after setting initializeWithValue to true - it is caused by defaultValue option not being set, that is returned when there is no key in storage (attempt to fetch returned null), check sandbox with example: https://codesandbox.io/s/trusting-gauss-yz8rv2?file=/src/App.tsx
if you'll set initializeWithValue option to true - there will be no undefined in state typings.
Is that true? I was experimenting with this the other day, and even with initializeWithValue set to trueundefined was still a possible type of the value. It was actually a bit annoying. Any thoughts @xobotyi ?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, working with
useLocalStorageValue
is frustrating as it specifically disables setting a value tonull
, yet forcesnull
to be a type the value can be. It also forcesundefined
to be a type value can be. I can understand the reasoning of not wanting to allow null for JSON parse/stringify reasons as mentioned in the docs, but I feel like it needs to be consistent. Since I'm setting my own parse/stringify, I was able to get null to be a valid value, but now it's forcingundefined
to be an option, event if I setinitializeWithValue
to true. So in order for my types to be correct, I have to wrap the hook with a custom hook that coalesces the value to null if undefined. What's the reasoning behind forcing both null and undefined?Beta Was this translation helpful? Give feedback.
All reactions