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.

useStorage doesn't serialize if initial value is null

See original GitHub issue

Describe 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.

image

It does serialize correctly if the initial object is an object.

Reproduction

https://play.vueuse.org/#N4IgDghgxg1hDmBTAziAXAbVAOwgW0XRADcBXRAWgBNE8BLEAGhGQHtSAnKQtEU7MDHhMQNZFA50wAFzqtsRAGrkABABFadFQAoAFhAA2AMxV1sKgGIdE2KLoCUp5CogqaxRAdZgz8FaVkDOmkATxcDLwB3ZxD2FWlWFUjJaUQVAFVsOg8OZEMVZTSAGToAIw4ISRQVI1YOAtUAJhUAMhUAZhFOAyJdaWkwZDQAemH+QXgAOihWPGGyShp6YaDS4bMaAA9JvAArVABfRhx8HhAAAQXSZERh5H1rKhE2Tm4icaERMQkpWXkiADKD0QVAaiHSN38gWCdBQky6HB6vD6AyGow+Uxmc0u5Gut3ulRB5wArJMAAyTACM62wW0mKDwk32ICOJwIRBxiDxwxm1me7C4ZwxXxQPxkcgUvAAwqwIogoH9zKwTCgbthZPlCioZXgwKxkDD5BlobIUAikSAUYMRmMBEJprNhpzubzECTyVSaXSGUzDgBdZhGOgGM1oUAAQTAYEmC3QoFSuoMEFSRAAPFRsiooEnkMgALwAHRA8EkoPgEDAFEpKl211kRhCFGQ0kq0iLAD4C+YVKnSgEEuZztm6LBCyxENIAS3UpSiyp2wCJ-FEs3JNh4Knhn3+vJO93e-2jUOgqOizdJ9PEI0O4vpMuVKxSrtN9uB3uVD3X0fh6fxxfk4g7Q3kuCQqNgpARC+h7YHuXapro7QLogiCbgh749mA1jtsAwDNgBBwHJumGIHum4ZsQ7bPGK0hEHQup1HewD+DcU51AgaQHDUHCzCoADkzo3DydSILxXZdjM2DNqYWQagYKh5iowBdh+H4QGgKhFmYwRFowykqaU6ngREekHAA3GJ2CjBJUl4ak8nMYgrEVEg2i8aUSa8Yw0kwoY9jmbY8g2Ze9l4k57Gue5ECeWBEEGH5FlGPwCoSio55TgBlLaI4SndrZiAxoYqgKUWpSsLMRb+QcFldoltiKqlE7pakjRZYpel5QVBhFW13aqepRaxDpekfgZGkgOUrBFiZlUJUl9VpZe7StTlH4dcQhVpApRkGDNCgEQcQA

System Info

vueuse

Used Package Manager

npm

Validations

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
okxiaoliang4commented, May 17, 2022

My recommendation.

import type { StorageSerializers } from '@vueuse/core'
import { useStorage } from '@vueuse/core'

useStorage('my-info', null, localStorage, {
  serializer: StorageSerializers.object,
})

or you can wrap your useStorage e.g

import type { StorageSerializers } from '@vueuse/core'
import { useStorage } from '@vueuse/core'

export function wrapUseStorage<T>(...args: Parameters<typeof useStorage>) {
  return useStorage<T>(args[0], args[1], args[2], {
    serializer: StorageSerializers.object,
    ...args[3],
  })
}

wrapUseStorage('my-info', null)
1reaction
JMaylorcommented, May 17, 2022

Yes I did that, but it’s still totally unexpected behaviour. Also, the object I’m storing doesn’t require custom serialization. It just behaves differently depending on what the initial value is. That makes no sense to me.

If 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:

// all of the 4 declarations below initialise as null.
const myDate = useStorage('my-date', null)
const myCounter = useStorage('my-counter', null)
const myName = useStorage('my-name', null)
const myInfo = useStorage("my-info", null)

// myDate is a Date object and wants to parse / format ISO strings as the serialiser
myDate.value = new Date()

// myCounter is an integer and wants to use parseInt
myCounter.value = 12

// myName is a string and needs no serialisation
myName.value = 'jmaylor'

// myInfo is an object and wants to use JSON serialisation
myInfo.value = {
  address: 'somewhere',
  age: 50
}

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 that myInfo is going to be an object. This is why the serialisation has to be explicitly stated at initialisation. Hope this helps.

Read more comments on GitHub >

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

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