3.1.0 • Published 1 year ago

kea-localstorage v3.1.0

Weekly downloads
2,276
License
MIT
Repository
github
Last release
1 year ago

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({
    persistedValue: [0, { persist: true, prefix: 'example', separator: '_' }, {
      changeThing: (_, 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']

storageKey

Pass a storageKey, to override the key used for storage. This allows multiple logics to share the same value. For example to have all keyed logics store a reducer globally.

const someLogic = kea([
  key(props => props.key), // not used for localstorage, overridden by storageKey
  reducers(({ actions }) => ({
    persistedValue: [0, { persist: true, storageKey: 'my.global.key' }, {
      [actions.change]: (_, payload) => payload.value
    }]
  }))
])
3.1.0

1 year ago

3.0.0

2 years ago

3.0.0-alpha.2

2 years ago

3.0.0-alpha.1

2 years ago

3.0.0-alpha.0

2 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

5 years ago

0.3.0

5 years ago

1.0.0-beta.6

5 years ago

1.0.0-beta.5

5 years ago

1.0.0-beta.4

5 years ago

1.0.0-beta.3

5 years ago

1.0.0-beta.2

5 years ago

1.0.0-beta.1

5 years ago

1.0.0-beta.0

5 years ago

0.2.0

6 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago