安装:
npm install --save redux-smart-cache
使用
1.配置持久化的实现,以及缓存的时间
例如:在react-native app里,我们可以这样做
// 实现了缓存的存储,返回Promise,result是voidcacheConfig{ const data = JSON await AsyncStorage}// 实现缓存的读取,返回Pormise,result是一个json对象cacheConfig{ const value = await AsyncStorage return JSON}// 设置userinfo这个state的缓存时间为1小时cacheConfigcacheDueTime = userinfo: 3600 * 1000// 设置默认的缓存时间为200秒cacheConfigdefaultDueTime = 200 * 1000
2. Action
{ return // 配置尝试,成功,失败三种type types: 'USERINFO_LOAD_ATTEMPT' 'USERINFO_LOAD_SUCCESS' 'USERINFO_LOAD_FAILURE' // 实现callAPI接口,是个promise接口 callAPI: async { const res = await return userinfo: resbodyuserinfo //结果是否有效,该属性为true,则使用cache,否则不做任何处理 effective: resbodyerrorNo == 0 } // 第一个type的action载体 payload: userId attempting: true // 是否强制更新,如果为false,则使用cache forceUpdate: false }
3.配置reducer, combineReducer, 及使用中间件
{ }const rootReducer = const enhancers = const store =
依赖
redux
es-2015
es6