8月的最后一天了,那就打响最后一炮吧!
我们介绍了Flux框架,我们打算在接下来的项目里使用Redux框架,这两天简单学习了下Redux。打算结合一个Demo来讲解。
还是先来说说概念吧。
这让同构应用开发变得非常容易。来自服务端的 state 可以在无需编写更多代码的情况下被序列化并注入到客户端中。由于是单一的 state tree ,调试也变得非常容易。在开发中,你可以把应用的 state 保存在本地,从而加快开发速度。此外,受益于单一的 state tree ,以前难以实现的如“撤销/重做”这类功能也变得轻而易举。
我们再来介绍下Redux中有三个基本概念,Action,Reducer,Store。
Action 本质上是 JavaScript 普通对象。我们约定,action 内必须使用一个字符串类型的 type 字段来表示将要执行的动作。多数情况下,type 会被定义成字符串常量。当应用规模越来越大时,建议使用单独的模块或文件来存放 action。
代码如下:
export const addTodo = (num) => { return { type: 'INCREMENT', num: num, } } export const decTodo = (num) => { return { type: 'DECREMENT', num: num, } } export const updateText = (text) => { return { type: 'TEXT_UPDATE', text: text, } }
reducer 就是一个纯函数,接收旧的 state 和 action,返回新的 state。
(previousState, action) => newState保持 reducer 纯净非常重要。永远不要在 reducer 里做这些操作:
reduceNum如下:
const todo = (state=0, action) => { switch (action.type) { case 'INCREMENT': return state + 1 case 'DECREMENT': return state - 1 default: return state//返回原来的state } } export default todo
const todo = (state="", action) => { switch (action.type) { case 'TEXT_UPDATE': //新的state return action.text default: //返回原来的state return state } } export default todo
import { combineReducers } from 'redux' import reduceNum from './reduceNum' import reduceText from './reduceText' const todoApp = combineReducers({ num:reduceNum, text:reduceText }) export default todoApp
Store 就是把action和reduce联系到一起的对象。Store 有以下职责:
import reducers from './reducers' import { createStore } from 'redux' let initState = { num: 0,text:""}; let store = createStore(reducers,initState);createStore() 的第二个参数是可选的, 用于设置 state 初始状态。
好了,概念就介绍到这儿,贴下Demo的源码。
入口组件:
/** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from 'react-native'; import { createStore } from 'redux' import { Provider } from 'react-redux' import reducers from './reducers' import AddTodo from './containers/AddTodo' class reduxdemo extends Component { render() { // let initState = { num: 0,text:""}; let store = createStore(reducers); return ( <Provider store={store}> <AddTodo /> </Provider> ) } } AppRegistry.registerComponent('reduxdemo', () => reduxdemo);
视图组件:AddTodo
import React,{Component} from 'react' import { AppRegistry, StyleSheet, Text, View, TouchableHighlight, Dimensions, } from 'react-native'; import { connect,Provider } from 'react-redux' import { addTodo,decTodo,updateText } from '../actions' const {width, height} = Dimensions.get('window'); class AddTodo extends Component { constructor(props) { super(props); } render() { const { dispatch,num ,text} = this.props // alert(num); return ( <View style={styles.container}> <TouchableHighlight style={styles.itemView} underlayColor="red" onPress={()=>{dispatch(addTodo(num))}}> <Text style={styles.itemText}> 点击我就+1 </Text> </TouchableHighlight> <TouchableHighlight style={styles.itemView} underlayColor="red" onPress={()=>{dispatch(decTodo(num))}}> <Text style={styles.itemText}> 点击我就-1 </Text> </TouchableHighlight> <TouchableHighlight style={styles.itemView} underlayColor="red" onPress={()=>{dispatch(updateText('测试'))}}> <Text style={styles.itemText}> 修改Text的值 </Text> </TouchableHighlight> <Text style={{paddingTop:10,color:'red'}}> 当前的Num值是:{num} , 当前的Text值是:{text} </Text> </View> ) } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f2f2f2', marginTop:20, }, itemView:{ backgroundColor:'grey', height:44, width:width, justifyContent:'center', marginTop:10, }, itemText:{ fontSize:15, color:'#ffffff', textAlign:'left', marginLeft:20, }, }); //selector:这是你自己编写的一个函数。这个函数声明了你的组件需要整个 store 中的哪一部分数据作为自己的 props。 function selector(state) { return { num: state.num , text:state.text } } // 包装 component ,注入 dispatch 和 state 到其默认的 connect(selector)(App) 中; export default connect(selector)(AddTodo);
Demo运行的效果如下:
代码结构目录如下:
写在后面的话:
Redux真不是一两句话可以说清楚的,这里提供了一个简单的完整Demo,希望可以帮助到大家。
参考:http://cn.redux.js.org/index.html