Skip to content
On this page

useWebStorage

Web Storage API

Parameters

typescript
import { useWebStorage } from '@elonehoo/pistachio'

const webStorage = useWebStorage(type, serializer?, delayMs?)
import { useWebStorage } from '@elonehoo/pistachio'

const webStorage = useWebStorage(type, serializer?, delayMs?)
ParametersTypeRequiredDefaultDescription
typelocalStorage | sessionStoragetruestorage type, it will return always the same object if called multiple times with the same type
serializerSerializerfalseJSONcustom serializer, it uses stringify() and parse()
delayMsnumberfalse10debounce time for save to storage

State

The useWebStorage function exposes the following reactive state:

typescript
import { useWebStorage } from '@elonehoo/pistachio'

const { supported, quotaError, store } = useWebStorage()
import { useWebStorage } from '@elonehoo/pistachio'

const { supported, quotaError, store } = useWebStorage()
StateTypeDescription
supportedbooleanif the store is supported, basically only false when on safari private tab
quotaErrorRef<boolean>true if the quota was exceeded on save
storeStorageStorage object being used, this is not localStorage or sessionStorage, although it shares similar interface

Methods

The useWebStorage function exposes the following methods:

typescript
import { useWebStorage } from '@elonehoo/pistachio'

const { remove } = useWebStorage()
import { useWebStorage } from '@elonehoo/pistachio'

const { remove } = useWebStorage()
SignatureDescription
removeRemoves store from cache

Released under the MIT License.