@zero-dependency/storage
v1.3.4
Published
Web storage wrapper
Downloads
17
Maintainers
Readme
@zero-dependency/storage
Installation
npm install @zero-dependency/storage
yarn add @zero-dependency/storage
pnpm add @zero-dependency/storage
Usage
import { LocalStorage } from '@zero-dependency/storage'
interface User {
id: number
name: string
}
const storageKey = 'users'
const storage = new LocalStorage<User[]>(storageKey, [])
console.log(storage.initialValue) // []
storage.write((prevValue) => [...prevValue, { id: 1, name: 'John' }])
console.log(storage.value) // [{ id: 1, name: 'John' }]
storage.reset()
console.log(storage.value) // []
// https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event#examples
window.addEventListener('storage', (event) => {
if (event.key === storageKey) {
// do something
}
})