@plq/use-persisted-state
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -1,15 +0,12 @@ | ||
# [0.2.0](https://github.com/Akurganow/use-persisted-state/compare/v0.1.2...v%s) (2020-05-28) | ||
### [0.2.1](https://github.com/Akurganow/use-persisted-state/compare/v0.2.0...v0.2.1) (2020-05-29) | ||
### Features | ||
### Bug Fixes | ||
* **release:** add dotenv ([32dcbb2](https://github.com/Akurganow/use-persisted-state/commit/32dcbb2)) | ||
* **init:** don't call setState when init with initialValue ([cb17cfb](https://github.com/Akurganow/use-persisted-state/commit/cb17cfbcdf8731ff7695311b35771c2736a11580)) | ||
## [0.1.2](https://github.com/Akurganow/use-persisted-state/compare/v0.1.1...v0.1.2) (2019-10-25) | ||
# [0.2.0](https://github.com/Akurganow/use-persisted-state/compare/v0.1.0...v0.2.0) (2020-05-29) | ||
## [0.1.1](https://github.com/Akurganow/use-persisted-state/compare/v0.1.0...v0.1.1) (2019-10-25) | ||
# [0.1.0](https://github.com/Akurganow/use-persisted-state/compare/v0.0.10...v0.1.0) (2019-09-24) | ||
### Bug Fixes | ||
@@ -20,53 +17,6 @@ | ||
* **tests:** button text ([35bd20d](https://github.com/Akurganow/use-persisted-state/commit/35bd20d)) | ||
### Features | ||
* **lint:** init ([f54e050](https://github.com/Akurganow/use-persisted-state/commit/f54e050)) | ||
* **readme:** add codesandbox example ([d161824](https://github.com/Akurganow/use-persisted-state/commit/d161824)) | ||
* **tests:** add clear test ([e119c94](https://github.com/Akurganow/use-persisted-state/commit/e119c94)) | ||
## [0.0.10](https://github.com/Akurganow/use-persisted-state/compare/v0.0.9...v0.0.10) (2019-09-10) | ||
### Bug Fixes | ||
* **check value:** check key in ([9b3ab38](https://github.com/Akurganow/use-persisted-state/commit/9b3ab38)) | ||
## [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) | ||
### Bug Fixes | ||
* **empty key:** check that key in persisted storage ([aae8daa](https://github.com/Akurganow/use-persisted-state/commit/aae8daa)) | ||
## [0.0.7](https://github.com/Akurganow/use-persisted-state/compare/v0.0.5...v0.0.7) (2019-09-06) | ||
## [0.0.6](https://github.com/Akurganow/use-persisted-state/compare/v0.0.5...v0.0.6) (2019-09-06) | ||
## [0.0.5](https://github.com/Akurganow/use-persisted-state/compare/0.0.4...0.0.5) (2019-09-06) | ||
## [0.0.4](https://github.com/Akurganow/use-persisted-state/compare/0.0.3...0.0.4) (2019-09-06) | ||
### Bug Fixes | ||
* **initial value:** move initialPersist into hook ([98eced6](https://github.com/Akurganow/use-persisted-state/commit/98eced6)) | ||
## [0.0.3](https://github.com/Akurganow/use-persisted-state/compare/0.0.2...0.0.3) (2019-09-06) | ||
## [0.0.2](https://github.com/Akurganow/use-persisted-state/compare/0.0.1...0.0.2) (2019-09-04) | ||
### Bug Fixes | ||
* **eslint:** missed plugin ([ea67e59](https://github.com/Akurganow/use-persisted-state/commit/ea67e59)) | ||
## 0.0.1 (2019-09-04) | ||
### Bug Fixes | ||
* **clear:** dispatchEvent after clear ([4177143](https://github.com/Akurganow/use-persisted-state/commit/4177143)) | ||
@@ -78,1 +28,6 @@ * **example:** add clear ([4f382a0](https://github.com/Akurganow/use-persisted-state/commit/4f382a0)) | ||
### Features | ||
* **lint:** init ([f54e050](https://github.com/Akurganow/use-persisted-state/commit/f54e050)) | ||
* **readme:** add codesandbox example ([d161824](https://github.com/Akurganow/use-persisted-state/commit/d161824)) | ||
* **tests:** add clear test ([e119c94](https://github.com/Akurganow/use-persisted-state/commit/e119c94)) |
@@ -1,4 +0,9 @@ | ||
import * as React from 'react'; | ||
import isFunction from './utils/is-function'; | ||
export default function createPersistedState(storageKey, storage = window.localStorage) { | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const react_1 = require("react"); | ||
const is_function_1 = __importDefault(require("./utils/is-function")); | ||
function createPersistedState(storageKey, storage = window.localStorage) { | ||
const safeStorageKey = `persisted_state_hook:${storageKey}`; | ||
@@ -22,6 +27,6 @@ const clear = () => { | ||
} | ||
const [state, setState] = React.useState(initialOrPersistedValue); | ||
const [state, setState] = react_1.useState(initialOrPersistedValue); | ||
const setPersistedState = (newState) => { | ||
let newValue; | ||
if (isFunction(newState)) { | ||
if (is_function_1.default(newState)) { | ||
newValue = newState(state); | ||
@@ -42,8 +47,10 @@ setState(newValue); | ||
}; | ||
React.useEffect(() => { | ||
react_1.useEffect(() => { | ||
const handleStorage = (event) => { | ||
if (event.key === safeStorageKey) { | ||
const newState = JSON.parse(event.newValue); | ||
const newValue = newState && key in newState ? newState[key] : initialValue; | ||
setState(newValue); | ||
const newValue = newState && key in newState ? newState[key] : null; | ||
if (newValue && newValue !== initialValue) { | ||
setState(newValue); | ||
} | ||
} | ||
@@ -60,2 +67,3 @@ }; | ||
} | ||
exports.default = createPersistedState; | ||
//# sourceMappingURL=index.js.map |
@@ -0,5 +1,8 @@ | ||
"use strict"; | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
export default function isArguments(value) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isArguments(value) { | ||
return (toString.call(value) === '[object Arguments]' || (value !== null && typeof value === 'object' && 'callee' in value)); | ||
} | ||
exports.default = isArguments; | ||
//# sourceMappingURL=is-arguments.js.map |
@@ -0,5 +1,8 @@ | ||
"use strict"; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
export default function isArray(value) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isArray(value) { | ||
return Array.isArray(value) || toString.call(value) === '[object Array]'; | ||
} | ||
exports.default = isArray; | ||
//# sourceMappingURL=is-array.js.map |
@@ -1,8 +0,13 @@ | ||
import isObject from './is-object'; | ||
import isArray from './is-array'; | ||
import isArguments from './is-arguments'; | ||
export default function isEmpty(value) { | ||
if (isObject(value)) { | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const is_object_1 = __importDefault(require("./is-object")); | ||
const is_array_1 = __importDefault(require("./is-array")); | ||
const is_arguments_1 = __importDefault(require("./is-arguments")); | ||
function isEmpty(value) { | ||
if (is_object_1.default(value)) { | ||
const length = Object.getOwnPropertyNames(value).length; | ||
if (length === 0 || (length === 1 && isArray(value)) || (length === 2 && isArguments(value))) { | ||
if (length === 0 || (length === 1 && is_array_1.default(value)) || (length === 2 && is_arguments_1.default(value))) { | ||
return true; | ||
@@ -14,2 +19,3 @@ } | ||
} | ||
exports.default = isEmpty; | ||
//# sourceMappingURL=is-empty.js.map |
@@ -1,4 +0,7 @@ | ||
export default function isFunction(value) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isFunction(value) { | ||
return toString.call(value) === '[object Function]' || typeof value === 'function'; | ||
} | ||
exports.default = isFunction; | ||
//# sourceMappingURL=is-function.js.map |
@@ -0,5 +1,8 @@ | ||
"use strict"; | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
export default function isObject(value) { | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function isObject(value) { | ||
return Object(value) === value; | ||
} | ||
exports.default = isObject; | ||
//# sourceMappingURL=is-object.js.map |
{ | ||
"name": "@plq/use-persisted-state", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "useState hook with persistance in storage", | ||
@@ -49,28 +49,27 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"@release-it/conventional-changelog": "^1.1.0", | ||
"@release-it/conventional-changelog": "^1.1.4", | ||
"@testing-library/jest-dom": "^5.9.0", | ||
"@testing-library/react": "^10.0.4", | ||
"@testing-library/react-hooks": "^2.0.1", | ||
"@types/jest": "^24.0.18", | ||
"@types/react": "^16.9.2", | ||
"@typescript-eslint/eslint-plugin": "^2.3.0", | ||
"@typescript-eslint/parser": "^2.3.0", | ||
"conventional-changelog-cli": "^2.0.23", | ||
"@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", | ||
"conventional-changelog-cli": "^2.0.34", | ||
"dotenv-cli": "^3.1.0", | ||
"eslint": "^6.4.0", | ||
"eslint-config-jest-enzyme": "^7.1.1", | ||
"eslint-config-prettier": "^6.3.0", | ||
"eslint-plugin-jest": "^23.13.1", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"eslint-plugin-react": "^7.14.3", | ||
"eslint": "^7.1.0", | ||
"eslint-plugin-jest": "^23.13.2", | ||
"eslint-plugin-react": "^7.20.0", | ||
"eslint-plugin-react-hooks": "^4.0.4", | ||
"jest": "^24.9.0", | ||
"prettier": "^2.0.5", | ||
"prettier-eslint": "^9.0.0", | ||
"jest": "^26.0.1", | ||
"jest-environment-jsdom-sixteen": "^1.0.3", | ||
"jest-localstorage-mock": "^2.4.2", | ||
"parcel": "^2.0.0-alpha.3.2", | ||
"react": "^16.9.0", | ||
"react-dom": "^16.9.0", | ||
"react-test-renderer": "^16.9.0", | ||
"react-dom": "^16.13.1", | ||
"react-test-renderer": "^16.13.1", | ||
"release-it": "^13.6.1", | ||
"ts-jest": "^24.1.0", | ||
"typescript": "^3.6.3" | ||
"ts-jest": "^26.0.0", | ||
"typescript": "^3.9.3" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import * as React from 'react' | ||
import React, { useState, useEffect } from 'react' | ||
@@ -9,3 +9,3 @@ import isFunction from './utils/is-function' | ||
storageKey: string, | ||
storage: Storage = window.localStorage | ||
storage: Storage = window.localStorage, | ||
): [<T>(key: string, initialValue: T) => UsePersistedState<T>, () => void] { | ||
@@ -34,3 +34,3 @@ const safeStorageKey = `persisted_state_hook:${storageKey}` | ||
const [state, setState] = React.useState<T>(initialOrPersistedValue) | ||
const [state, setState] = useState<T>(initialOrPersistedValue) | ||
@@ -55,3 +55,3 @@ const setPersistedState = (newState: React.SetStateAction<T>): void => { | ||
[key]: newValue, | ||
}) | ||
}), | ||
) | ||
@@ -63,9 +63,11 @@ | ||
React.useEffect(() => { | ||
useEffect(() => { | ||
const handleStorage = (event: StorageEvent): void => { | ||
if (event.key === safeStorageKey) { | ||
const newState = JSON.parse(event.newValue as string) | ||
const newValue = newState && key in newState ? newState[key] : initialValue | ||
const newValue = newState && key in newState ? newState[key] as T : null | ||
setState(newValue) | ||
if (newValue && newValue !== initialValue) { | ||
setState(newValue) | ||
} | ||
} | ||
@@ -72,0 +74,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
24
185
21269
30