├── .editorconfig ├── .gitignore ├── README.md ├── index.d.ts ├── package.json ├── src └── index.ts ├── tsconfig.json └── yarn.lock /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # dependencies 2 | /node_modules 3 | 4 | # production 5 | index.js 6 | 7 | # misc 8 | .DS_Store 9 | .idea 10 | .env.local 11 | .env.development.local 12 | .env.test.local 13 | .env.production.local 14 | 15 | npm-debug.log* 16 | yarn-debug.log* 17 | yarn-error.log* 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # React Persist Hook 2 | 3 | Hook for persisting and rehydrating state in the React app. 4 | 5 | ## Installation 6 | 7 | ```bash 8 | npm install react-persist-hook 9 | ``` 10 | or 11 | ```bash 12 | yarn add react-persist-hook 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```javascript 18 | import React from 'react' 19 | import usePersist from 'react-persist-hook' 20 | 21 | const Signin = () => { 22 | const [signinFormValues, setSigninFormValues] = usePersist('signin-form', 'localStorage', 400); 23 | 24 | const { userName, email } = signinFormValues || {}; 25 | const initValues = { userName, email }; 26 | 27 | const handleSubmit = ({ userName, email }) => { 28 | setSigninFormValues({ userName, email }); 29 | // rest of the submit method 30 | } 31 | 32 | return ( 33 |
36 | ) 37 | } 38 | ``` 39 | 40 | ## Props 41 | 42 | | Prop | Type | Default | Required | Description | 43 | |---------------|------------------------------------------|----------------|----------|-------------------------------------------------------| 44 | | key | string | | true | Unique storage key | 45 | | storageType | ['localStorage'|'sessionStorage'] | 'localStorage' | false | Storage type name | 46 | | debounceLimit | number | 250 | false | Time in milliseconds to debounce the state persisting | 47 | 48 | 49 | ## License 50 | [MIT](https://choosealicense.com/licenses/mit/) 51 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare const usePersist: (key: string, storageType?: "localStorage" | "sessionStorage" | undefined, debounceLimit?: number | undefined) => [any, Function]; 2 | export default usePersist; 3 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-persist-hook", 3 | "version": "1.0.7", 4 | "description": "Hook for persisting and rehydrating state in React app", 5 | "main": "index.js", 6 | "types": "index.d.ts", 7 | "scripts": { 8 | "build": "tsc && echo 'Finished building NPM package'", 9 | "test": "echo \"Error: no test specified\" && exit 1" 10 | }, 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/szlezak/react-persist-hook.git" 14 | }, 15 | "keywords": [ 16 | "react", 17 | "react-hook", 18 | "persist" 19 | ], 20 | "author": { 21 | "name": "Robert Szlezak", 22 | "email": "robertszlezak@gmail.com" 23 | }, 24 | "contributors": [ 25 | { 26 | "name": "Mark Polak", 27 | "email": "mwd@outlook.sk" 28 | } 29 | ], 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/szlezak/react-persist-hook/issues" 33 | }, 34 | "homepage": "https://github.com/szlezak/react-persist-hook#readme", 35 | "peerDependencies": { 36 | "react": "^16.0.0-0" 37 | }, 38 | "devDependencies": { 39 | "typescript": "^3.6.4", 40 | "@types/react": "^16.9.9", 41 | "@types/lodash.debounce": "^4.0.6" 42 | }, 43 | "dependencies": { 44 | "lodash.debounce": "^4.0.8" 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { useEffect, useRef, useState } from 'react'; 2 | import debounce from 'lodash.debounce'; 3 | 4 | const usePersist = ( 5 | key: string, 6 | storageType?: 'localStorage' | 'sessionStorage', 7 | debounceLimit?: number, 8 | ): [any, Function] => { 9 | const DEBOUNCE_LIMIT = debounceLimit || 250; 10 | const STORAGE_TYPE = storageType || 'localStorage'; 11 | 12 | const mounted = useRef