内容简介:参考:react native 中几乎绝大部分操作都是异步的,包括 读取本地存储。
参考: https://github.com/rt2zz/redux-persist 和
https://github.com/rt2zz/redux-persist/blob/master/docs/PersistGate.md
react native 中几乎绝大部分操作都是异步的,包括 读取本地存储。
而现实页面则是同步的,而且貌似没有特别容易的办法 让页面跟 读取后的数据同步(自动刷新)
redux-persis 项目就是为了这个目的而生的。
示范代码已经上传: https://github.com/sg552/demo_redux_with_async_storage.git
1. 安装
$ npm install redux-persist --save
并根据文档: http://siwei.me/blog/posts/react-15-redux 建立一个空白的redux项目。
2. 修改store.js (注意名字和路径跟官方文档略有不同)
import {createStore, combineReducers } from 'redux'
import countReducer from './reducers'
// persist store
import {persistStore, persistReducer } from 'redux-persist'
import storage from 'redux-persist/lib/storage'
const rootReducer = combineReducers({
countReducer: countReducer
})
const myReducer = persistReducer({
key: 'root',
storage
}, rootReducer)
export default () => {
let store = createStore(myReducer)
let persistor = persistStore(store)
return { store , persistor}
}
3. 修改index.js
/** @format */
import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
// 以下为 为调用Redux 而新增的代码
// 这句话虽然没有被显示调用,但是不加会报错, 在 const myProvider那里。
import React from 'react'
import { Provider } from 'react-redux'
import configureStore from './store'
// 导入redux-persist
import {PersistGate} from 'redux-persist/integration/react'
const {store, persistor} = configureStore()
const onBeforeLift = () => {
}
const myProvider = () => {
return (
<Provider store = {store}>
<PersistGate loading={null} persistor={persistor}
onBeforeLift={onBeforeLift}
>
<App />
</PersistGate>
</Provider>
)
}
AppRegistry.registerComponent(appName, () => myProvider);
4. 其他代码同 普通的 redux 项目(见DEMO ,或者 )
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
小米生态链战地笔记
小米生态链谷仓学院 / 中信出版集团 / 2017-5 / 56.00
2013年下半年,小米开始做一件事,就是打造一个生态链布局IoT(物联网);2016年年底,小米生态链上已经拥有了77家企业,生态链企业整体销售额突破100亿元。这3年,是小米生态链快速奔跑的3年,也是小米在商场中不断厮杀着成长的3年。 3年,77家生态链企业,16家年销售额破亿,4家独角兽公司,边实战,边积累经验。 小米生态链是一个基于企业生态的智能硬件孵化器。过去的3年中,在毫无先......一起来看看 《小米生态链战地笔记》 这本书的介绍吧!