redux 是一个用于管理 js 应用状态(state)的容器。好比组件 A 发生了变化,组件 B 要同时作出响应。常见的应用场景就是用户的登陆退出操做:未登陆状态,我的中心显示登陆按钮,在登陆页面进行了登陆后,须要在我的中心页面作出相应,显示我的信息。相似的产品有: vuex、flux、dva、mobx。vue
redux 常见为三个部分:react
在本应用里使用 redux 主要用于保存用户状态、保存首页数据。git
yarn add redux redux-persist react-redux
在 src 目录下建立 action 目录,目录中建立 index.js。github
这里建立 2 个应用中要用到的方法,login(登陆)、logout(退出);2 个测试 redux 是否生效的方法,add(加大数字)、cut(减小数字)。vuex
export function add(num) { // 每一个函数的返回值里面必须得有一个type值,Reducer就是根据type值改变作出相应 return { type: 'add', value: ++num } } export function cut(num) { return { type: 'cut', value: --num } } export function login(data) { return { type: 'login', data } } export function logout(data) { return { type: 'logout', data } }
在 src 目录下建立 reducer 目录,目录中建立 index.js/num.js/user.js。redux
index.jsreact-native
import { combineReducers } from 'redux'; import num from './num'; import user from './user'; //这里返回的combineReducers()就是 Store 的内容,后面想要得到的话,就是使用 store.user、store.num来得到对应store的数据 export default combineReducers({ num: num, user: user })
num.jsapp
const initState = {//初始化state内容 value: 0 } const setNumState = (state = initState, action) => { switch (action.type) {//根据type的不一样作出不一样的响应 case 'add': return { ...state, value: action.value, status: 'add' } case 'cut': return { ...state, value: action.value, status: 'cut' } default://必须得有default,用于返回非指望的状态 return state; } } export default setNumState;
user.jside
const initState = { isLogin: false, userData: {} } const setUserState = (state = initState, action) => { switch (action.type) { case 'login': return { ...state, isLogin: true, userData: action.data } case 'logout': return initState; default: return state; } } export default setUserState;
在 src 目录下建立 store 目录,目录中建立 index.js。函数
import { AsyncStorage } from 'react-native'; import { createStore } from 'redux'; import { persistStore, persistReducer } from 'redux-persist'; import hardSet from 'redux-persist/lib/stateReconciler/hardSet'; import reducer from '../reducer'; //配置redux-persist,但下次进入应用,就会从root里面得到数据 let persistConfig = { key: 'root', storage: AsyncStorage, stateReconciler: hardSet } const persistedReducer = persistReducer(persistConfig, reducer); export default function configureStore() { const store = createStore(persistedReducer); let persistor = persistStore(store); return { store, persistor }; }
import React, { Component } from 'react'; import { Provider } from 'react-redux'; import { PersistGate } from 'redux-persist/integration/react'; import configureStore from './store'; import Router from './router'; const store = configureStore(); export default class App extends Component { render() { return ( <Provider store={store.store} style={{ flex: 1 }}> <PersistGate loading={null} persistor={store.persistor}> <Router /> </PersistGate> </Provider> ); } };
import React from "react"; import { View, Text } from "react-native"; import { connect } from 'react-redux'; import { add, cut } from '../action'; class Home extends React.Component { render() { const { dispatch, value, navigation } = this.props; return ( <View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}> <Text onPress={() => navigation.push('Detail')}>Home Click Me</Text> <View style={{ flexDirection: 'row', width: 300, justifyContent: 'space-around' }}> <Text onPress={() => dispatch(add(value))} style={{ backgroundColor: 'blue', color: '#fff', fontSize: 18 }}>减小数字</Text> <Text style={{ fontSize: 20 }}>{value}</Text> <Text onPress={() => dispatch(cut(value))} style={{ backgroundColor: 'blue', color: '#fff', fontSize: 18 }}>加大数字</Text> </View> </View> ); } } const select = (store) => { return { value: store.num.value, } } export default connect(select)(Home);
效果图:
这里是一个集成了 router、redux、icons的基本工程,clone下来就能使用 https://github.com/hulinNeil/react-native-base;