1.2.1 • Published 1 year ago
use-storage-backed-state v1.2.1
useStorageBackedState

Custom React hook for storage backed persisted state. Check interactive demo.
Installation
npm install use-storage-backed-stateHow to use
import React from 'react'
import { useStorageBackedState } from 'use-storage-backed-state'
export const MyComponent = () => {
// 0: initialState
// 'count': localStorage key
const [count, setCount] = useStorageBackedState(0, 'count')
return (
<section>
<h1>
Value: <output>{count}</output>
</h1>
<button
onClick={() => {
setCount(count + 1)
}}
>
increment
</button>
<button
onClick={() => {
setCount(count - 1)
}}
>
decrement
</button>
</section>
)
}
Notes
Stores data in
localStorage.Works with
sessionStoragetoo.useStorageBackedState(…, …, sessionStorage)Realtime synchronization between multiple uses with the same
key. Even across tabs.You can opt out from storage and synchronization by passing
nullas the second argument or by omitting thekeyaltogether.useStorageBackedStatewill then behave similarly likeuseStatein that case.const [count, setCount] = useStorageBackedState(1)const [storeState, setStoreState] = useState(false) const [count, setCount] = useStorageBackedState( 1, storeState ? 'count' : null, )
1.2.1
1 year ago
1.2.0
2 years ago
1.1.1
2 years ago
1.1.0
2 years ago
1.1.3
2 years ago
1.1.2
2 years ago
1.0.4
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago
1.0.3
2 years ago
0.2.4
3 years ago
0.2.3
3 years ago
0.2.1
5 years ago
0.2.2
5 years ago
0.2.0
5 years ago
0.1.0
5 years ago
0.0.3
5 years ago
0.0.2
5 years ago
0.0.1
5 years ago