-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Thoughts on missing localStorage? #5
Comments
Respect for the type of issue you have created. This is what I call a way to approach problems – with questions. These aren't easy questions. Can you give me a few days to think about it and we can discuss them after that? |
I saw that type of thinking in your post: https://espen.codes/post/strictness-in-apis-or-why-hapi-rocks. Nice! |
const [value, setValue, isPersistent] = useLocalStorageState('key') What do you think about such an API? The hook will return an additional boolean to let you know if the data is currently being persisted in localStorage or not? |
I like it! Leave it up to the app developer whether or not they care :) |
I am done with the implementation. Can you check it out and tell me what you think? |
This looks great! |
Perfect. I am closing this issue then. If you have more valuable feedback please don't hesitate to write it. |
In certain contexts you may encounter browsers which have disabled localStorage.
If you're blocking cookies in Safari, for instance, simply trying to access
localStorage
(even for atypeof
check) will throw an error.In other cases/browsers, attempting to actually set an item will yield a quota exceeded error (they set the max quota size to 0 bytes).
An actual check for whether or not localStorage is "available and usable" would look something like:
Even with such a check, the "solution" isn't obvious - what should a library such as this one do in this case? Throw? Warn? Pretend like it isn't happening?
Given that you often use this as a "drop-in" replacement for
setState
, I would at least expect it to fall back to using in-memory state - but in that case, how far do you go? Try to implement postMessage calls for cross-tab/window polyfilling of the missingstorage
events?I'm raising this mostly to get your perspective, but I also feel that perhaps we should try to implement something that prevents this hook from potentially blocking the page from being rendered should the user have disabled localStorage.
The text was updated successfully, but these errors were encountered: