Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@plq/use-persisted-state

Akurganow2.5kMIT1.2.0TypeScript support: included

useState hook with persistence in storage

react, hook, storage, local-storage, session-storage, persistence, persist, sync

readme

usePersistedState

npm version Tests

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

import createPersistedState from '@plq/use-persisted-state'
import storage from '@plq/use-persisted-state/lib/storages/local-storage'

const [usePersistedState] = createPersistedState('example', storage)

export default function App() {
  const [count, setCount] = usePersistedState('count', 0)
  const handleIncrement = () => setCount(prevCount => prevCount + 1)

  return (
    <div>
      {count}
      <button onClick={handleIncrement}>+</button>
    </div>
  )
}

Edit @plq/use-persisted-state

Requirement

To use @plq/use-persisted-state, you must use `react@16.8.0` or greater which includes Hooks.

Clear Storage

import createPersistedState from '@plq/use-persisted-state'
import storage from '@plq/use-persisted-state/lib/storages/local-storage'

const [usePersistedState, clear] = createPersistedState('example', storage)

export default function App() {
  const [count, setCount] = usePersistedState('count', 0)
  const increment = () => setCount(prevCount => prevCount + 1)

  return (
    <div>
      {count}
      <button onClick={increment}>+</button>
      <button onClick={clear}>Clear</button>
    </div>
  )
}

Use sessionStorage

import createPersistedState from '@plq/use-persisted-state'
import storage from '@plq/use-persisted-state/lib/storages/session-storage'

const [usePersistedState, clear] = createPersistedState('example', storage)

Use async storage

import createPersistedState from '@plq/use-persisted-state'
// or
import { createAsyncPersistedState } from '@plq/use-persisted-state'
import { local } from '@plq/use-persisted-state/lib/storages/browser-storage'

const [usePersistedState, clear] = createPersistedState('example', local)

Use custom storage

The storage API is similar to the browser.storage but with a few differences

import createPersistedState from '@plq/use-persisted-state'

const storageListeners = new Set()

onChangeSomeStorage(event => {
  const changes = {
    [event.key]: {
      newValue: event.newValue,
      oldValue: event.oldValue,
    },
  }

  listeners.forEach(listener => {
    listener(changes)
  })
})

const myStorage = {
  get: keys => getItemsFromSomeStorage(keys),
  set: items => setItemsToSomeStorage(items),
  remove: keys => removeItemsFromSomeStorage(keys),
  onChanged: {
    addListener: listener => storageListeners.add(listener),
    removeListener: listener => storageListeners.delete(listener),
    hasListener: listener => storageListeners.has(listener),
  }
}

const [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.

changelog

1.1.5 (2023-10-08)

Bug Fixes

  • package.json peerDependencies and engines (5e48a2f)
  • Update gitignore (2b99bc4)

1.1.4 (2023-10-08)

1.1.3 (2023-10-08)

Bug Fixes

  • Github actions main job (4dd9df1)
  • Github actions main job (839aea7)
  • Github actions main job (1b40f22)
  • useStorageHandler missing useEffect deps (3266ba3)

1.1.2 (2021-02-09)

Bug Fixes

  • toString: IE11 fix (TypeError: Invalid calling object) (1514cd6)

1.1.1 (2020-12-18)

1.1.0 (2020-10-04)

Features

1.0.2 (2020-06-15)

Bug Fixes

  • async storage: setState in set hook (e287091)

1.0.1 (2020-06-14)

1.0.0 (2020-06-14)

Features

  • examples: add async storage example (5377681)
  • hook: support async storage (68195f7)
  • storages: add storages (9a8640b)

0.2.5 (2020-06-02)

Bug Fixes

  • clear: dispatch event with old value (38e05ef)

0.2.4 (2020-06-02)

Bug Fixes

  • storage listener: listen key remove (76d4bf5)

0.2.3 (2020-06-02)

Bug Fixes

  • storage listener: check values is not null (109b7a1)

0.2.2 (2020-05-29)

0.2.1 (2020-05-29)

Bug Fixes

  • init: don't call setState when init with initialValue (cb17cfb)

0.2.0 (2020-05-29)

0.1.0 (2019-09-24)

Bug Fixes

  • actions: comand name (06160e7)
  • package: email (18c2bd3)
  • tests: button text (35bd20d)
  • check value: check key in (9b3ab38)
  • empty key: check that key in persisted storage (aae8daa)
  • initial value: move initialPersist into hook (98eced6)
  • eslint: missed plugin (ea67e59)
  • clear: dispatchEvent after clear (4177143)
  • example: add clear (4f382a0)
  • examples: remove unneded exports (2dc5759)
  • examples: simplify (97dca4f)
  • examples: typos (4a10d2e)

Features