npm install @zero-dependency/storage
yarn add @zero-dependency/storage
pnpm add @zero-dependency/storage
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
}
})