use-local-storage
use-local-storage copied to clipboard
Failing tests for stale key/default value
Here added failing tests that are expected to work but fail; my other PR unfortunately is not great, I closed it and will add my current working code on top of this PR! Unfortunately it means some major code changes will happen on this PR, it's not a 1-line fix anymore.
Issue: I am finding it very troublesome to combine these two statements and I think they clash with each other:
- When the value in localStorage is undefined, return the
defaultValue
and write it to localStorage - When the value is set to
undefined
, remove the value from localStorage
It would seem like the logical way here is:
- When the value is set to
undefined
, remove the value from localStorage; hence return thedefaultValue
(but don't write it!)
And also only one of these two options should be true, I believe the first one but the current implementation is... mixed:
- Only write to localStorage when the value is manually changed, not when you are reading-only and the value is undefined (e.g. don't write on read)
- Write to localStorage on read if it's undefined, BUT then when writing an
undefined
manually it should automatically write thedefaultValue
. I don't like this, since "reading" is "writing", and "deleting" doesn't mean deleting anymore, but it's kind of how it works now.