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

Support false values with a true fallback value #48

Merged
merged 3 commits into from
Mar 21, 2020
Merged

Support false values with a true fallback value #48

merged 3 commits into from
Mar 21, 2020

Conversation

jaebradley
Copy link
Contributor

@jaebradley jaebradley commented Mar 13, 2020

Summary

This is a great utility hook, but I ran into some issues when storing booleans (specifically false with a true initial value).

Basically, when the local storage value is false (or more specifically, "false") and an initial value of true is specified, the value returned by the useLocalStorage hook is true.

There are certain UI cases where one would want the default behavior to be true but to still remember the case where the user has toggled that behavior to a false state (like remembering if a slideout is opened or closed).

I've gotten around this by using numerical values, but it would be great if this hook supported the case where the local storage value is false while the default behavior is true.

Reproducing the behavior

Here's a screenshot of the behavior

image

Here's a link to the underlying demo: https://stackblitz.com/edit/local-storage-false-example

Basically, toggle the button until the local storage value is false and then reload the page.

Discussion

I noticed that in a lot of places, the default value is only set / used if the parsed local storage value is falsy.

tryParse(localStorage.getItem(key)!) || initialValue

From the Storage#getItem MDN page it seems like the existence of a key is determined by whether null is returned by getItem.

image

Maybe it would be more appropriate to check if localStorage.getItem(key) is null before falling back to the initial value?

I know this is a breaking change, which would necessitate a v3.x.x release, but it might ultimately lead to more expected behavior?

@jharrilim
Copy link
Collaborator

Hi @jaebradley, thanks for your contribution! This should be appropriate to add as a patch as I'm sure the current behaviour is a bug.

@iamsolankiamit iamsolankiamit merged commit 14762a1 into rehooks:master Mar 21, 2020
@jaebradley jaebradley deleted the support-false-values-with-a-true-fallback-value branch March 27, 2020 14:30
@doytch
Copy link

doytch commented Apr 23, 2020

@jharrilim, was the intention of the 2.3.0 release to include this bugfix? The date stamps appear to show it being released four hours after this PR was merged, but looking at the actual code that's pulled down from NPM, it's still using the incorrect pre-PR behaviour. E.g.,

[12:19:10] rehook2.3.0 : cat node_modules/\@rehooks/local-storage/package.json | grep "\"version\":"
  "version": "2.3.0"
[12:19:13] rehook2.3.0 : cat node_modules/\@rehooks/local-storage/lib/use-localstorage.js | grep -A 2 "function useLocalStorage"
function useLocalStorage(key, initialValue) {
    const [localState, updateLocalState] = react_1.useState(tryParse(localStorage.getItem(key)) || initialValue);
    const onLocalStorageChange = (event) => {

Could you release a 2.3.1 that includes this fix?

@jharrilim
Copy link
Collaborator

Hi @doytch, yes I believe the fix should've been there. Unfortunately I do not have publish rights to @rehooks on npm as I am not a part of this org. @iamsolankiamit may have to do it instead.

@doytch
Copy link

doytch commented Apr 23, 2020

Ahhh OK, thanks for the clarification; I was just going off of the Github releases page 😆

iamsolankiamit added a commit that referenced this pull request Apr 30, 2020
* 'master' of github.com:rehooks/local-storage:
  Add prepublishOnly script
  Create npmpublish.yml
  Patch for issue #53 (#54)
  Fix for #50 \n\nAlso refactored a test that was causing a warning (#51)
  Support false values with a true fallback value (#48)
@iamsolankiamit
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants