web icon indicating copy to clipboard operation
web copied to clipboard

Custom (de)serializer for use*StorageValue hooks

Open dantman opened this issue 3 years ago • 1 comments

New Features

Custom serializer/deserializer options for the use*StorageValue hooks.

This would allow the use of custom serialization for (local|session)Storage data instead of the default JSON.

What is the new or updated feature that you are suggesting?

  • A deserializer option that will replace the default parse function for use*StorageValue
  • A serializer option that will replace the default stringify funciton for use*StorageValue

Why should this feature be included?

Sometimes you need custom serialization for (local|session)Storage.

  • You're already storing simple data in a simplified serialization format (e.g. 1,2,3 instead of [1,2,3]).
  • You have non-primitive data that will not be correctly deserialized by the default JSON.parse and need to use a custom revier/replacer function.
  • You are working with small binary data and need to base64 encode it instead of JSON.

react-use's useLocalStorage supports a custom (de)serializer. So it's difficult to switch to @react-hooks/web when it's missing the functionality you were using. This is a bigger deal since react-use's useSessionStorage does not support a (de)serializer. So if you want to switch something to sessionStorage but need a custom (de)serializer then you have no option.

dantman avatar Sep 21 '22 19:09 dantman

I've written a PR for this https://github.com/react-hookz/web/pull/913.

dantman avatar Sep 21 '22 19:09 dantman

Closed with #913

ArttuOll avatar Nov 12 '22 19:11 ArttuOll