useStorage doesn't serialize if initial value is null
See original GitHub issueDescribe the bug
If the initial value is using null, useStorage will work just fine for simple types like strings, but it will put [Object object] into local storage if it’s set to an object.
It does serialize correctly if the initial object is an object.
Reproduction
System Info
vueuse
Used Package Manager
npm
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a VueUse issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion.
- The provided reproduction is a minimal reproducible example of the bug.
Issue Analytics
- State:
- Created a year ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to ignore a property in class if null, using json.net
You can do this to ignore all nulls in an object you're serializing, and any null properties won' ...
Read more >useStorage - VueUse
Custom Serialization #. By default, useStorage will smartly use the corresponding serializer based on the data type of provided default value. For example,...
Read more >System.Text.Json: Ignore null values while serializing #39152
When serving JSON for frontends, then default values of value types are extremely important. It's not like null.
Read more >Hooks API Reference - React
It accepts a new state value and enqueues a re-render of the component. ... Unlike the setState method found in class components, useState...
Read more >The Null and Drop Values — colander 1.8.3 documentation
The injection of the colander.null value into a serialization when a default doesn't exist for the corresponding node is not a behavior shared...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
My recommendation.
or you can wrap your
useStorage
e.gIf you’re initial value is null, how are we meant to know what to do with the value in localStorage? Suppose I have the below 4 refs, all using localStorage, and all starting off as null:
At the point of declaring all 4 of the above refs, all initialised as null, each of these will require different serialisation later down the line. We are unable to infer that
myCounter
is going to be an integer, or thatmyInfo
is going to be an object. This is why the serialisation has to be explicitly stated at initialisation. Hope this helps.