@plq/use-persisted-state
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -0,1 +1,8 @@ | ||
### [0.2.4](https://github.com/Akurganow/use-persisted-state/compare/v0.2.3...v0.2.4) (2020-06-02) | ||
### Bug Fixes | ||
* **storage listener:** listen key remove ([76d4bf5](https://github.com/Akurganow/use-persisted-state/commit/76d4bf5227920e0739fc8292ffc7e657bb03f7c5)) | ||
### [0.2.3](https://github.com/Akurganow/use-persisted-state/compare/v0.2.2...v0.2.3) (2020-06-02) | ||
@@ -2,0 +9,0 @@ |
@@ -48,2 +48,14 @@ "use strict"; | ||
const handleStorage = (event) => { | ||
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] : null; | ||
if (oldValue !== initialValue) | ||
setState(initialValue); | ||
} | ||
if (event.key === safeStorageKey && event.newValue !== null) { | ||
@@ -50,0 +62,0 @@ let newState = null; |
{ | ||
"name": "@plq/use-persisted-state", | ||
"version": "0.2.3", | ||
"version": "0.2.4", | ||
"description": "useState hook with persistance in storage", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -62,2 +62,16 @@ import React, { useState, useEffect } from 'react' | ||
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) { | ||
@@ -64,0 +78,0 @@ let newState = null |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24503
218