Installations
npm install @plq/use-persisted-state
Developer Guide
Typescript
Yes
Module System
CommonJS
Min. Node Version
>=16.0.0
Node Version
20.8.0
NPM Version
10.1.0
Score
68.4
Supply Chain
93.9
Quality
76.2
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (91.92%)
JavaScript (8.08%)
Developer
Akurganow
Download Statistics
Total Downloads
83,844
Last Day
56
Last Week
466
Last Month
1,838
Last Year
23,048
GitHub Statistics
20 Stars
623 Commits
6 Forks
5 Watching
14 Branches
2 Contributors
Bundle Size
19.32 kB
Minified
2.93 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.2.0
Package Id
@plq/use-persisted-state@1.2.0
Unpacked Size
65.16 kB
Size
14.72 kB
File Count
86
NPM Version
10.1.0
Node Version
20.8.0
Publised On
09 Oct 2023
Total Downloads
Cumulative downloads
Total Downloads
83,844
Last day
-73.5%
56
Compared to previous day
Last week
-37.4%
466
Compared to previous week
Last month
4.7%
1,838
Compared to previous month
Last year
55.4%
23,048
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
25
usePersistedState
Persists the state to localStorage, sessionStorage or any custom storage
Features
- Persist the state to
localStorage
,sessionStorage
or almost anything else implements storage API - The state factory takes as many keys as needed, so you don't have to call the factory for each variable
- Written with the TypeScript, the definitions go with the library
- No third-party dependencies
Example
1import createPersistedState from '@plq/use-persisted-state' 2import storage from '@plq/use-persisted-state/lib/storages/local-storage' 3 4const [usePersistedState] = createPersistedState('example', storage) 5 6export default function App() { 7 const [count, setCount] = usePersistedState('count', 0) 8 const handleIncrement = () => setCount(prevCount => prevCount + 1) 9 10 return ( 11 <div> 12 {count} 13 <button onClick={handleIncrement}>+</button> 14 </div> 15 ) 16}
Requirement
To use @plq/use-persisted-state
, you must use react@16.8.0
or greater which includes Hooks.
Clear Storage
1import createPersistedState from '@plq/use-persisted-state' 2import storage from '@plq/use-persisted-state/lib/storages/local-storage' 3 4const [usePersistedState, clear] = createPersistedState('example', storage) 5 6export default function App() { 7 const [count, setCount] = usePersistedState('count', 0) 8 const increment = () => setCount(prevCount => prevCount + 1) 9 10 return ( 11 <div> 12 {count} 13 <button onClick={increment}>+</button> 14 <button onClick={clear}>Clear</button> 15 </div> 16 ) 17}
Use sessionStorage
1import createPersistedState from '@plq/use-persisted-state' 2import storage from '@plq/use-persisted-state/lib/storages/session-storage' 3 4const [usePersistedState, clear] = createPersistedState('example', storage)
Use async storage
1import createPersistedState from '@plq/use-persisted-state' 2// or 3import { createAsyncPersistedState } from '@plq/use-persisted-state' 4import { local } from '@plq/use-persisted-state/lib/storages/browser-storage' 5 6const [usePersistedState, clear] = createPersistedState('example', local)
Use custom storage
The storage API is similar to the browser.storage but with a few differences
1import createPersistedState from '@plq/use-persisted-state'
2
3const storageListeners = new Set()
4
5onChangeSomeStorage(event => {
6 const changes = {
7 [event.key]: {
8 newValue: event.newValue,
9 oldValue: event.oldValue,
10 },
11 }
12
13 listeners.forEach(listener => {
14 listener(changes)
15 })
16})
17
18const myStorage = {
19 get: keys => getItemsFromSomeStorage(keys),
20 set: items => setItemsToSomeStorage(items),
21 remove: keys => removeItemsFromSomeStorage(keys),
22 onChanged: {
23 addListener: listener => storageListeners.add(listener),
24 removeListener: listener => storageListeners.delete(listener),
25 hasListener: listener => storageListeners.has(listener),
26 }
27}
28
29const [usePersistedState, clear] = createPersistedState('example', myStorage)
Storage adapters
localStorage @plq/use-persisted-state/lib/storages/local-storage
- Useful for average web application
sessionStorage @plq/use-persisted-state/lib/storages/session-storage
- Useful for average web application
browser.storage @plq/use-persisted-state/lib/storages/browser-storage
- Only for web extensions.
- Don't forget to set up polyfill if you want to run extension in Chrome browser.
- To use this storage you need to include the "storage" permission in your
manifest.json
file
chrome.storage @plq/use-persisted-state/lib/storages/chrome-storage
- Only for Chrome-based web extensions.
- If you develop extension that will be run only in Chrome browser you can use this storage without polyfill.
- You must declare the "storage" permission in the extension manifest to use this storage.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
dependency not pinned by hash detected -- score normalized to 2
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:23: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:25: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:34: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:57: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:59: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:68: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:91: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:93: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:101: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/main.yml:114: update your workflow using https://app.stepsecurity.io/secureworkflow/Akurganow/use-persisted-state/main.yml/master?enable=pin
- Info: 0 out of 10 GitHub-owned GitHubAction dependencies pinned
- Info: 3 out of 3 npmCommand dependencies pinned
Reason
8 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-prr3-c3m5-p7q2
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-7hpj-7hhx-2fgx
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/main.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
3.5
/10
Last Scanned on 2025-01-27
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