内容简介:参考: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协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入理解计算机系统
Randal E.Bryant、David O'Hallaron / 龚奕利、雷迎春 / 中国电力出版社 / 2004-5-1 / 85.00元
从程序员的视角,看计算机系统! 本书适用于那些想要写出更快、更可靠程序的程序员。通过掌握程序是如何映射到系统上,以及程序是如何执行的,读者能够更好的理解程序的行为为什么是这样的,以及效率低下是如何造成的。粗略来看,计算机系统包括处理器和存储器硬件、编译器、操作系统和网络互连环境。而通过程序员的视角,读者可以清晰地明白学习计算机系统的内部工作原理会对他们今后作为计算机科学研究者和工程师的工作有......一起来看看 《深入理解计算机系统》 这本书的介绍吧!