Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Map & Set to useLocalStorage control #309

Conversation

AlecsFarias
Copy link

@AlecsFarias AlecsFarias commented Apr 10, 2023

-> enable useLocalStorage hook to work with Map and Set
-> check instance types of initialValue to work with return type on readValue
-> create stringify function to work separately with type checking and string build

issue: #308

Comment on lines +35 to +45
const parsed = parseJSON(item) as T

//considering that the initial value is a Map, convert object to map
if(initialValue instanceof Map) {
return new Map(Object.entries(parsed as Object)) as T
}

//considering that the initial value is a Set, convert the array to set
if(initialValue instanceof Set) {
return new Set(parsed as Array<any>) as T
}
Copy link
Author

@AlecsFarias AlecsFarias Apr 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna write this here because I think it is more like to be a question on this pr than a part of it's description.

'Why not put this Map and Set creation logic inside parseJSON function ? "

I did this way because I think that it was no good to add a new parameter to parseJSON function just to say that it would be a Map or Set. Considering that is no more than this 2 instance checks I think that it would have no problem to stay this way, but, open for tips and discussions !

@DevBrasil
Copy link

Hi, very usefull function

@alexandreferris
Copy link

finally someone did it 🙌🏻

@juliencrn
Copy link
Owner

Hi you all, good feature indeed. As the PR's base branch is outdated, I moved this code there #439.
It should be in the next release
Thanks!

@juliencrn juliencrn closed this Jan 24, 2024
juliencrn added a commit that referenced this pull request Jan 25, 2024
* ✨ Add serialization support for use-*-storage hooks

* ✅ Add unit tests about #437 (code from #276 by @sskirby)

* ✨ Explicity allow () => T as initial value (missing in #436)

* ✨ Add Date, Set & Map support to use*Storage (#309 by @AlecsFarias)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improvements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants