use-localstorage-reducer
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

use-localstorage-reducer

Sync the state of a React reducer with LocalStorage. Typesafe and SSR compatible.

Installation

npm install use-localstorage-reducer

Usage

import { useLocalStorageReducer } from 'use-localstorage-reducer'

function App() {
  const [state, dispatch] = useLocalStorageReducer(
	'storage-key',
	reducerFunc,
  { name: '', age: 0}, // initial state
	[], // optional array of middleware functions
	[], // optional array of afterware functions
  {
    serializer: (value: S) => JSON.stringify(value), // optional serializer function
    deserializer?: (value: string) => JSON.parse(value), // optional deserializer function
    initializeWithValue: true // optional initialize with value from LocalStorage (default: true, set false for SSR)
  },
}

License

MIT

Package Sidebar

Install

npm i use-localstorage-reducer

Weekly Downloads

6

Version

0.0.2

License

MIT

Unpacked Size

9.47 kB

Total Files

5

Last publish

Collaborators

  • camin-mccluskey