0.0.4 • Published 4 years ago

redux-localstorage-sync v0.0.4

Weekly downloads
3
License
MIT
Repository
github
Last release
4 years ago

redux-localstorage-sync

描述: 此项目是redux的一个enhancer,在redux-localstorage的基础上开发,用于将redux中的状态同步到localstorage中,相比于redux-localstorage功能有以下增强:

  1. 对redux的状态写入到localstorage添加了函数节流(debounce),默认为100ms。
  2. 添加了初始化时localstorage同步到redux状态的选择,默认情况下syncLocalstorageToRedux为true,自动开启同步。
  3. 增强对路径(path)访问的支持,当设置paths为 'foo.bar.dhh'时,即将redux状态中的state.bar.dhh的值同步到localstorage中。

安装

$ npm install redux-localstorage-sync

使用

import {compose, createStore} from 'redux';
import persistState from 'redux-localstorage-sync'

const enhancer = compose(
  /* [middlewares] */,
  persistState(/*paths, config*/),
)

const store = createStore(/*reducer, [initialState]*/, enhancer)

参数

参数typedescription默认值
Pathsnull | String | Array默认情况下,将把redux中所有的状态同步到localstorage中,如果项目的状态庞大,建议配置具体的路径;当设置paths为 'foo.bar.dhh'(等效于'foo.bar.dhh')时,将redux状态中的state.bar.dhh的值同步到localstorage中,当在数组指定多个值的时候,注意属性相互覆盖的问题 。Null
configObject见下面说明

config

主要的参数如下

参数类型描述默认值
namespaceString设置localstorage的值'redux-localstorage-sync'
debounceTimenumber设置函数节流的时间100
syncLocalstorageToReduxBoolean是否将localstorage的值在初始化时同步到reduxtrue