question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] useStorage default value `null` get's written to localstorage and read as `"null"`

See original GitHub issue

If 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:21 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
adamdehavencommented, Jul 22, 2021

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.

1reaction
jeffrey-dot-licommented, Jul 22, 2021

Wait ok I think I didn’t completely fix the issue in my pull request - I didn’t realize that there was useStorage and useLocalStorage I think I only fixed for useStorage not the other one. Just to clarify @antfu whats supposed to be the difference between the two? I’ll pull request a fix for useLocalStorage as well thanks for bringing this up

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found