use-local-storage-state
Advanced tools
Comparing version 18.1.0 to 18.1.1
@@ -5,2 +5,6 @@ import { useRef, useMemo, useEffect, useCallback, useSyncExternalStore } from 'react'; | ||
export default function useLocalStorageState(key, options) { | ||
// istanbul ignore next | ||
if (typeof useSyncExternalStore === 'undefined') { | ||
throw new TypeError(`You are using React 17 or below. Install with "npm install use-local-storage-state@17".`); | ||
} | ||
const defaultValue = options === null || options === void 0 ? void 0 : options.defaultValue; | ||
@@ -25,5 +29,5 @@ // SSR support | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
return useClientLocalStorageState(key, defaultValue, options === null || options === void 0 ? void 0 : options.storageSync, serializer === null || serializer === void 0 ? void 0 : serializer.parse, serializer === null || serializer === void 0 ? void 0 : serializer.stringify); | ||
return useBrowserLocalStorageState(key, defaultValue, options === null || options === void 0 ? void 0 : options.storageSync, serializer === null || serializer === void 0 ? void 0 : serializer.parse, serializer === null || serializer === void 0 ? void 0 : serializer.stringify); | ||
} | ||
function useClientLocalStorageState(key, defaultValue, storageSync = true, parse = parseJSON, stringify = JSON.stringify) { | ||
function useBrowserLocalStorageState(key, defaultValue, storageSync = true, parse = parseJSON, stringify = JSON.stringify) { | ||
const initialDefaultValue = useRef(defaultValue).current; | ||
@@ -30,0 +34,0 @@ // store default value in localStorage: |
{ | ||
"name": "use-local-storage-state", | ||
"version": "18.1.0", | ||
"version": "18.1.1", | ||
"description": "React hook that persist data in localStorage", | ||
@@ -12,3 +12,3 @@ "license": "MIT", | ||
"email": "hello@astoilkov.com", | ||
"url": "http://astoilkov.com" | ||
"url": "https://astoilkov.com" | ||
}, | ||
@@ -52,11 +52,11 @@ "keywords": [ | ||
"devDependencies": { | ||
"@size-limit/preset-small-lib": "^7.0.8", | ||
"@size-limit/preset-small-lib": "^8.0.0", | ||
"@testing-library/react": "^13.3.0", | ||
"@types/jest": "^28.1.1", | ||
"@types/react": "^18.0.12", | ||
"@types/react-dom": "^18.0.4", | ||
"@typescript-eslint/eslint-plugin": "^5.27.1", | ||
"@typescript-eslint/parser": "^5.27.1", | ||
"@types/jest": "^28.1.6", | ||
"@types/react": "^18.0.17", | ||
"@types/react-dom": "^18.0.6", | ||
"@typescript-eslint/eslint-plugin": "^5.33.0", | ||
"@typescript-eslint/parser": "^5.33.0", | ||
"confusing-browser-globals": "^1.0.11", | ||
"eslint": "^8.17.0", | ||
"eslint": "^8.21.0", | ||
"eslint-config-strictest": "^0.4.0", | ||
@@ -67,5 +67,5 @@ "eslint-formatter-pretty": "^4.0.0", | ||
"eslint-plugin-react-hooks": "^4.5.0", | ||
"eslint-plugin-unicorn": "^42.0.0", | ||
"jest": "^28.1.1", | ||
"jest-environment-jsdom": "^28.1.1", | ||
"eslint-plugin-unicorn": "^43.0.2", | ||
"jest": "^28.1.3", | ||
"jest-environment-jsdom": "^28.1.3", | ||
"np": "^7.6.1", | ||
@@ -76,5 +76,5 @@ "prettier": "^2.6.2", | ||
"react-test-renderer": "^18.1.0", | ||
"size-limit": "^7.0.8", | ||
"size-limit": "^8.0.0", | ||
"superjson": "^1.9.1", | ||
"ts-jest": "^28.0.4", | ||
"ts-jest": "^28.0.7", | ||
"typescript": "^4.7.3" | ||
@@ -81,0 +81,0 @@ }, |
@@ -24,3 +24,3 @@ # `use-local-storage-state` | ||
- Actively maintained for the past 2 years — see [contributions](https://github.com/astoilkov/use-local-storage-state/graphs/contributors) page. | ||
- Actively maintained for the past 2 years — see [contributors](https://github.com/astoilkov/use-local-storage-state/graphs/contributors) page. | ||
- React 18 concurrent rendering support. | ||
@@ -39,3 +39,2 @@ - SSR support. | ||
const [todos, setTodos] = useLocalStorageState('todos', { | ||
ssr: true, | ||
defaultValue: ['buy avocado', 'do 50 push-ups'] | ||
@@ -50,3 +49,3 @@ }) | ||
You can experiment with the example [here](https://codesandbox.io/s/todos-example-use-local-storage-state-pewbql?file=/src/App.tsx). | ||
You can experiment with the example [here](https://codesandbox.io/s/todos-example-use-local-storage-state-tzbfhl?file=/src/App.tsx). | ||
@@ -59,3 +58,2 @@ ```tsx | ||
const [todos, setTodos] = useLocalStorageState('todos', { | ||
ssr: true, | ||
defaultValue: ['buy avocado'] | ||
@@ -86,3 +84,3 @@ }) | ||
<details> | ||
<summary id="is-persistent">Notify the user when <code>localStorage</code> isn't saving the data using the <code>`isPersistent`</code> property</summary> | ||
<summary id="is-persistent">Notify the user when <code>localStorage</code> isn't saving the data using the <code>isPersistent</code> property</summary> | ||
<p></p> | ||
@@ -137,3 +135,3 @@ | ||
### `useLocalStorageState(key: string, options?: LocalStorageOptions)` | ||
#### `useLocalStorageState(key: string, options?: LocalStorageOptions)` | ||
@@ -144,3 +142,3 @@ Returns `[value, setValue, { removeItem, isPersistent }]` when called. The first two values are the same as `useState()`. The third value contains two extra properties: | ||
### `key` | ||
#### `key` | ||
@@ -153,3 +151,3 @@ Type: `string` | ||
### `options.defaultValue` | ||
#### `options.defaultValue` | ||
@@ -162,3 +160,3 @@ Type: `any` | ||
### `options.storageSync` | ||
#### `options.storageSync` | ||
@@ -171,3 +169,3 @@ Type: `boolean` | ||
### `options.serializer` | ||
#### `options.serializer` | ||
@@ -178,2 +176,6 @@ Type: `{ stringify, parse }` | ||
JSON does not serialize `Date`, `Regex`, or `BigInt` data. You can pass in [superjson](https://github.com/blitz-js/superjson) or other `JSON`-compatible serialization library for more advanced serialization. | ||
JSON does not serialize `Date`, `Regex`, or `BigInt` data. You can pass in [superjson](https://github.com/blitz-js/superjson) or other `JSON`-compatible serialization library for more advanced serialization. | ||
## Related | ||
- [`use-session-storage-state`](https://github.com/astoilkov/use-session-storage-state) — A clone of this library but for `sessionStorage`. |
@@ -8,2 +8,6 @@ "use strict"; | ||
function useLocalStorageState(key, options) { | ||
// istanbul ignore next | ||
if (typeof react_1.useSyncExternalStore === 'undefined') { | ||
throw new TypeError(`You are using React 17 or below. Install with "npm install use-local-storage-state@17".`); | ||
} | ||
const defaultValue = options === null || options === void 0 ? void 0 : options.defaultValue; | ||
@@ -28,6 +32,6 @@ // SSR support | ||
// eslint-disable-next-line react-hooks/rules-of-hooks | ||
return useClientLocalStorageState(key, defaultValue, options === null || options === void 0 ? void 0 : options.storageSync, serializer === null || serializer === void 0 ? void 0 : serializer.parse, serializer === null || serializer === void 0 ? void 0 : serializer.stringify); | ||
return useBrowserLocalStorageState(key, defaultValue, options === null || options === void 0 ? void 0 : options.storageSync, serializer === null || serializer === void 0 ? void 0 : serializer.parse, serializer === null || serializer === void 0 ? void 0 : serializer.stringify); | ||
} | ||
exports.default = useLocalStorageState; | ||
function useClientLocalStorageState(key, defaultValue, storageSync = true, parse = parseJSON, stringify = JSON.stringify) { | ||
function useBrowserLocalStorageState(key, defaultValue, storageSync = true, parse = parseJSON, stringify = JSON.stringify) { | ||
const initialDefaultValue = (0, react_1.useRef)(defaultValue).current; | ||
@@ -34,0 +38,0 @@ // store default value in localStorage: |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
26085
10
384
171
0