@plq/use-persisted-state
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -0,1 +1,8 @@ | ||
### [0.2.3](https://github.com/Akurganow/use-persisted-state/compare/v0.2.2...v0.2.3) (2020-06-02) | ||
### Bug Fixes | ||
* **storage listener:** check values is not null ([109b7a1](https://github.com/Akurganow/use-persisted-state/commit/109b7a18f433f8b19f1c5e857c6c3021b26a0ad5)) | ||
### [0.2.2](https://github.com/Akurganow/use-persisted-state/compare/v0.2.1...v0.2.2) (2020-05-29) | ||
@@ -2,0 +9,0 @@ |
@@ -48,6 +48,12 @@ "use strict"; | ||
const handleStorage = (event) => { | ||
if (event.key === safeStorageKey) { | ||
const newState = JSON.parse(event.newValue); | ||
if (event.key === safeStorageKey && event.newValue !== null) { | ||
let newState = null; | ||
try { | ||
newState = JSON.parse(event.newValue); | ||
} | ||
catch (e) { | ||
console.error('use-persisted-state: Can\'t parse new value from storage', e); | ||
} | ||
const newValue = newState && key in newState ? newState[key] : null; | ||
if (newValue && newValue !== initialValue) { | ||
if (newValue !== null) { | ||
setState(newValue); | ||
@@ -54,0 +60,0 @@ } |
{ | ||
"name": "@plq/use-persisted-state", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "useState hook with persistance in storage", | ||
@@ -17,2 +17,3 @@ "main": "lib/index.js", | ||
"test": "jest --verbose", | ||
"test:watch": "npm test -- --watchAll", | ||
"lint": "eslint ./src/**/*.ts", | ||
@@ -52,8 +53,8 @@ "release": "npm run build && dotenv release-it", | ||
"@testing-library/jest-dom": "^5.9.0", | ||
"@testing-library/react": "^10.0.4", | ||
"@testing-library/react": "^10.0.5", | ||
"@testing-library/react-hooks": "^3.2.1", | ||
"@types/jest": "^25.2.3", | ||
"@types/react": "^16.9.35", | ||
"@typescript-eslint/eslint-plugin": "^3.0.2", | ||
"@typescript-eslint/parser": "^3.0.2", | ||
"@typescript-eslint/eslint-plugin": "^3.1.0", | ||
"@typescript-eslint/parser": "^3.1.0", | ||
"conventional-changelog-cli": "^2.0.34", | ||
@@ -68,10 +69,10 @@ "dotenv-cli": "^3.1.0", | ||
"jest-localstorage-mock": "^2.4.2", | ||
"parcel": "^2.0.0-alpha.3.2", | ||
"parcel": "^2.0.0-nightly.277", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.13.1", | ||
"react-test-renderer": "^16.13.1", | ||
"release-it": "^13.6.1", | ||
"ts-jest": "^26.0.0", | ||
"release-it": "^13.6.2", | ||
"ts-jest": "^26.1.0", | ||
"typescript": "^3.9.3" | ||
} | ||
} |
@@ -62,7 +62,14 @@ import React, { useState, useEffect } from 'react' | ||
const handleStorage = (event: StorageEvent): void => { | ||
if (event.key === safeStorageKey) { | ||
const newState = JSON.parse(event.newValue as string) | ||
if (event.key === safeStorageKey && event.newValue !== null) { | ||
let newState = null | ||
try { | ||
newState = JSON.parse(event.newValue) | ||
} catch (e) { | ||
console.error('use-persisted-state: Can\'t parse new value from storage', e) | ||
} | ||
const newValue = newState && key in newState ? newState[key] as T : null | ||
if (newValue && newValue !== initialValue) { | ||
if (newValue !== null) { | ||
setState(newValue) | ||
@@ -69,0 +76,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
22669
196