Gathering detailed insights and metrics for use-persisted-state
Gathering detailed insights and metrics for use-persisted-state
Gathering detailed insights and metrics for use-persisted-state
Gathering detailed insights and metrics for use-persisted-state
@types/use-persisted-state
TypeScript definitions for use-persisted-state
use-hashed-state
Safely store data in localstorage with an unique key of any type, even a component
px-use-persisted-state
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
@utilityjs/use-persisted-state
A React hook that provides a SSR-friendly multi-tab persistent state.
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
npm install use-persisted-state
Typescript
Module System
Node Version
NPM Version
97.9
Supply Chain
92.1
Quality
75.3
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
16,979,165
Last Day
2,178
Last Week
119,272
Last Month
432,885
Last Year
3,840,926
MIT License
1,382 Stars
33 Commits
96 Forks
8 Watchers
24 Branches
7 Contributors
Updated on Jun 16, 2025
Minified
Minified + Gzipped
Latest Version
0.3.3
Package Id
use-persisted-state@0.3.3
Size
4.78 kB
NPM Version
6.14.8
Node Version
14.15.1
Published on
Nov 19, 2020
Cumulative downloads
Total Downloads
Last Day
-81.8%
2,178
Compared to previous day
Last Week
-19.8%
119,272
Compared to previous week
Last Month
50.4%
432,885
Compared to previous month
Last Year
20.5%
3,840,926
Compared to previous year
1
1
A custom React Hook that provides a multi-instance, multi-tab/browser shared and persistent state.
use-persisted-state
is not a hook itself, but is a factory that accepts a storage key
and an optional storage provider (default = localStorage
) and returns a hook
that you can use as a direct replacement for useState
.
???? Persists the state to localStorage
???? Syncs between tabs and/or browser windows
???? Shares state w/multiple hooks on a page
To use use-persisted-state
, you must use react@16.8.0
or greater which includes Hooks.
1$ npm i use-persisted-state
Let's take a look at how you can use use-persisted-state
.
Here we have an example of a typical up/down counter.
1import { useState } from 'react'; 2 3const useCounter = initialCount => { 4 const [count, setCount] = useState(initialCount); 5 6 return { 7 count, 8 increment: () => setCount(currentCount => currentCount + 1), 9 decrement: () => setCount(currentCount => currentCount - 1), 10 }; 11}; 12 13export default useCounter;
Let's replace the import of react
with an import from use-persisted-state
.
And we'll call createPersistedState
(the factory function).
This will return a useCounterState
hook that we can use in place of useState
.
The complete code is as follows.
1import createPersistedState from 'use-persisted-state'; 2const useCounterState = createPersistedState('count'); 3 4const useCounter = initialCount => { 5 const [count, setCount] = useCounterState(initialCount); 6 7 return { 8 count, 9 increment: () => setCount(currentCount => currentCount + 1), 10 decrement: () => setCount(currentCount => currentCount - 1), 11 }; 12}; 13 14export default useCounter;
The state is shared with any other hook using the same key, either on the same page, across tabs, or even browser windows.
For example, open two copies of your app in two tabs or even two windows. Any changes to state in one tab will be rendered on the other tab.
You can also close the browser and the next time you run your app, the state will be rendered as it was before you closed your browser.
MIT Licensed
Thanks goes to these wonderful people (emoji key):
Donavon West ???? ⚠️ ???? ???? ???? ???? ???? ???? | Karol Majewski ???? | Octave Raimbault ???? | Dennis Morello ???? | Florent ???? | Mark Adamson ???? | Vitor Dino ???? |
This project follows the all-contributors specification. Contributions of any kind welcome!
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/23 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
47 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More