[bug] useStorage default value `null` get's written to localstorage and read as `"null"`
See original GitHub issueIf you set null
as the default value of useStorage
, this value get’s written to the localStorage on initial execution and is then read as the string "null"
. This behaviour was most likely introduced with #528 and I suspect it isn’t intended.
All versions >=4.11.1 are affected.
Example:
import { useStorage } from "@vueuse/core";
const test = useStorage("key", null);
console.log("Key:", typeof test.value, test.value);
// First load (expected behaviour)
// > Key: object null
// Subsequent loads (broken)
// > Key: string "null"
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:21 (12 by maintainers)
Top Results From Across the Web
[object Object] after storing object after null in useLocalStorage
@mxmvshnvsk This issue is caused because null is typed as 'any', which is basically 'string', by default when sent to local storage.
Read more >Javascript LocalStorage Null Bug - Stack Overflow
I suspect if you use the browser's devtools to look at what's in local storage, you'll find an entry for "1" with the...
Read more >useStorage - VueUse
Please note when you provide null as the default value, useStorage can't assume the data type from it. In this case, you can...
Read more >Using localStorage with React Hooks - LogRocket Blog
After importing the custom Hook, we can use it and pass along the unique key and the default value, which, in this case,...
Read more >chrome.storage - Chrome Developers
The Storage API is asynchronous with bulk read and write operations. ... storage.session: Holds data in memory for the duration of a browser...
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
Right – I don’t want to store
null
(and you are correct, localStorage is key (string) / value (string)So again, if a user passes
null
it should NEVER be converted to a string and stored as"null"
So something definitely needs to be changed/updated.
Wait ok I think I didn’t completely fix the issue in my pull request - I didn’t realize that there was
useStorage
anduseLocalStorage
I think I only fixed foruseStorage
not the other one. Just to clarify @antfu whats supposed to be the difference between the two? I’ll pull request a fix foruseLocalStorage
as well thanks for bringing this up