@plq/use-persisted-state
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -0,1 +1,3 @@ | ||
## [0.0.9](https://github.com/Akurganow/use-persisted-state/compare/v0.0.8...v0.0.9) (2019-09-10) | ||
## [0.0.8](https://github.com/Akurganow/use-persisted-state/compare/v0.0.7...v0.0.8) (2019-09-09) | ||
@@ -2,0 +4,0 @@ |
@@ -9,11 +9,14 @@ import * as React from 'react'; | ||
}; | ||
let initialPersist = storage.getItem(safeStorageKey); | ||
initialPersist = initialPersist ? JSON.parse(initialPersist) : {}; | ||
const usePersistedState = (key, initialValue) => { | ||
let initialPersist; | ||
try { | ||
const persist = storage.getItem(safeStorageKey); | ||
initialPersist = persist ? JSON.parse(persist) : {}; | ||
} | ||
catch (ignore) { | ||
initialPersist = {}; | ||
} // eslint-disable-line no-empty | ||
let initialOrPersistedValue = initialValue; | ||
if (initialPersist) { | ||
try { | ||
initialOrPersistedValue = initialPersist[key] || initialValue; | ||
} | ||
catch (ignore) { } // eslint-disable-line no-empty | ||
if (initialPersist && initialPersist[key]) { | ||
initialOrPersistedValue = initialPersist[key] || initialValue; | ||
} | ||
@@ -43,3 +46,3 @@ const [state, setState] = React.useState(initialOrPersistedValue); | ||
const newState = JSON.parse(event.newValue); | ||
const newValue = newState ? newState[key] : initialValue; | ||
const newValue = newState && newState[key] ? newState[key] : initialValue; | ||
setState(newValue); | ||
@@ -46,0 +49,0 @@ } |
{ | ||
"name": "@plq/use-persisted-state", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "Like useState hook but with persistance in storage", | ||
@@ -17,3 +17,3 @@ "main": "lib/index.js", | ||
"test": "jest --verbose", | ||
"release": "release-it", | ||
"release": "npm run build && release-it", | ||
"examples": "npm run build:watch & parcel examples/index.html" | ||
@@ -20,0 +20,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
20628
34
184