Socket
Socket
Sign inDemoInstall

kea-localstorage

Package Overview
Dependencies
9
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    kea-localstorage

Store reducer state in localStorage with Kea


Version published
Weekly downloads
14K
increased by60.65%
Maintainers
1
Install size
45.3 kB
Created
Weekly downloads
 

Readme

Source

NPM Version

Perstist Kea reducers in localstorage

  • kea-localstorage 1.1 works with kea 2.4+
  • kea-localstorage 1.0 works with kea 1.0+
  • kea-localstorage 0.1 works with kea 0.27+

Read the documentation for Kea

Installation

Install via yarn or npm

yarn add kea-localstorage
npm install --save kea-localstorage

Then add it to the context:

import { localStoragePlugin } from 'kea-localstorage'
import { resetContext } from 'kea'

resetContext({
  plugins: [ localStoragePlugin ]
})

Usage

To use it, make sure your logic store has a defined path. Then just pass { persist: true } as an option to your reducer, like so:

const someLogic = kea({
  path: () => ['scenes', 'something', 'foobar'], // NEEDED!

  actions: () => ({
    change: value => ({ value })
  }),

  reducers: ({ actions }) => ({
    persistedValue: [0, PropTypes.number, { persist: true }, {
      [actions.change]: (_, payload) => payload.value
    }]
  })
})

Options

You may optionally configure the plugin by passing in some options:

import { localStoragePlugin } from 'kea-localstorage'
import { resetContext } from 'kea'

resetContext({
  plugins: [
    localStoragePlugin({
      // in case you want to replace this, e.g. for tests or non browser environments
      storageEngine: window.localStorage,

      // added before all paths in localStorage's keys
      prefix: 'example',

      // to change the symbol that concats path parts
      separator: '_'
    })
  ]
})

With the above configuration all persisted reducers will now be save in the path: example_scenes_something_foobar

To use a different prefix/separator locally for specific reducers

const someLogic = kea({
  path: () => ['scenes', 'something', 'foobar'],

  reducers: ({ actions }) => ({
    // somewhere in your kea logic reducers
    persistedValue: [0, { persist: true, prefix: 'example', separator: '_' }, {
      [actions.change]: (_, payload) => payload.value
    }]
  })
})

Now the persistedValue will not be saved in scenes.something.foobar, but in example_scenes_something_foobar

Get the original default of the reducer

Under the hood kea-localstorage overrides the defaults value for your reducer with whatever was stored in localstorage. In case you need to access the original default, it's stored here:

logic.cache.localStorageDefaults['reducerKey']

FAQs

Last updated on 26 Jun 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc