astoilkov / use-local-storage-state

React hook that persists data in localStorage
MIT License
1.1k stars 41 forks source link

Support superjson serialization #49

Closed tianhuil closed 2 years ago

tianhuil commented 2 years ago

Hi @astoilkov: thanks for this great package! Here is a feature I'm using that I hope we can incorporate!

JSON does not serialize Date, Regex, or BigInt data. This PR allows users to pass in superjson or other JSON-compatible serialization classes for more advanced serialization.

See README.md and test.tsx

astoilkov commented 2 years ago

Wow! Thanks for the big contribution! That's well written and a nice addition. I should admit I have been working on something similar for the library. I would need a little time to review everything. Are you using your fork for now or am I blocking you on some project?

tianhuil commented 2 years ago

I'm using my fork for now -- but I'd love if it could be merged so that I can get all of your updates. Glad you liked it @astoilkov!

tianhuil commented 2 years ago

Any interest in merging this @astoilkov?

astoilkov commented 2 years ago

Hi. I've been working on a storage option — https://github.com/astoilkov/use-local-storage-state/tree/storage-option. This is something I've been tinkering with for a long time (as I mentioned in my first comment).

I won't merge your pull request but with my implementation, it will be possible to implement the same thing.

It's hard for me to not merge the work of others but I think the storage option has some benefits:

As not merging is something I've done before I am planning on making a Contributing.md file where I explain why and recommend to people to write me before making a contribution.

I'm sorry for the delay. I hope you understand. I'm trying to make my business work and this library is something I do for my love of programming. I'm delaying the work consciously because I should pay my bills first.

astoilkov commented 2 years ago

My storage option implementation failed. It has too many drawbacks and it's hard to work with. I will be implementing your proposal.

It will take some time because I'm also working on a rewrite for React 18 support. I want to work on the new implementation as well.

astoilkov commented 2 years ago

I'm ready with the implementation. The new serializer option is mainly inspired by your work. I've merged your work and I thank you.

Note that the new serializer option is only available in the latest release which supports React 18 but not React 17 and below.

usersina commented 1 year ago

Note that the new serializer option is only available in the latest release which supports React 18 but not React 17 and below.

Why is it so though?

astoilkov commented 1 year ago

The React 18 version is a full rewrite of the React 17 implementation. It was easier to implement this in the new code. If I did bring the feature in React 17, it would have been more than twice the work.

TheMoonDawg commented 1 year ago

Thanks so much for supporting this! I realized today that my filters were breaking because we were storing Date objects. This solved my woes. 🙌

astoilkov commented 1 year ago

Awesome to hear this!

Out of curiosity, where are you using the library?