Skip to content

Commit

Permalink
fix(storage listener): listen key remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Akurganow committed Jun 2, 2020
1 parent 826ca90 commit 76d4bf5
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ export default function createPersistedState(

useEffect(() => {
const handleStorage = (event: StorageEvent): void => {
if (event.key === safeStorageKey && event.newValue === null && event.oldValue !== null) {
let oldState = null

try {
oldState = JSON.parse(event.oldValue)
} catch (e) {
console.error('use-persisted-state: Can\'t parse old value from storage', e)
}

const oldValue = oldState && key in oldState ? oldState[key] as T : null

if (oldValue !== initialValue) setState(initialValue)
}

if (event.key === safeStorageKey && event.newValue !== null) {
let newState = null

Expand Down

0 comments on commit 76d4bf5

Please sign in to comment.