Redux系列01:从一个简单例子了解action、store、reducer
来源:互联网 发布:医疗器械国家数据库 编辑:程序博客网 时间:2024/05/17 06:06
先看例子
其实,redux的核心概念就是store、action、reducer,从调用关系来看如下所示
store.dispatch(action) --> reducer(state, action) --> final state
可以先看下面的极简例子有个感性的认识,下面会对三者的关系进行简单介绍
// reducer方法, 传入的参数有两个// state: 当前的state// action: 当前触发的行为, {type: 'xx'}// 返回值: 新的statevar reducer = function(state, action){ switch (action.type) { case 'add_todo': return state.concat(action.text); default: return state; }};// 创建store, 传入两个参数// 参数1: reducer 用来修改state// 参数2(可选): [], 默认的state值,如果不传, 则为undefinedvar store = redux.createStore(reducer, []);// 通过 store.getState() 可以获取当前store的状态(state)// 默认的值是 createStore 传入的第二个参数console.log('state is: ' + store.getState()); // state is:// 通过 store.dispatch(action) 来达到修改 state 的目的// 注意: 在redux里,唯一能够修改state的方法,就是通过 store.dispatch(action)store.dispatch({type: 'add_todo', text: '读书'});// 打印出修改后的stateconsole.log('state is: ' + store.getState()); // state is: 读书store.dispatch({type: 'add_todo', text: '写作'});console.log('state is: ' + store.getState()); // state is: 读书,写作
store、reducer、action关联
可以结合上面的代码来看下面几段解释
store:对flux有了解的同学应该有所了解,store在这里代表的是数据模型,内部维护了一个state变量,用例描述应用的状态。store有两个核心方法,分别是
getState
、dispatch
。前者用来获取store的状态(state),后者用来修改store的状态。// 创建store, 传入两个参数// 参数1: reducer 用来修改state// 参数2(可选): [], 默认的state值,如果不传, 则为undefinedvar store = redux.createStore(reducer, []);// 通过 store.getState() 可以获取当前store的状态(state)// 默认的值是 createStore 传入的第二个参数console.log('state is: ' + store.getState()); // state is:// 通过 store.dispatch(action) 来达到修改 state 的目的// 注意: 在redux里,唯一能够修改state的方法,就是通过 store.dispatch(action)store.dispatch({type: 'add_todo', text: '读书'});
action:对行为(如用户行为)的抽象,在redux里是一个普通的js对象。redux对action的约定比较弱,除了一点,action必须有一个
type
字段来标识这个行为的类型。所以,下面的都是合法的action{type:'add_todo', text:'读书'}{type:'add_todo', text:'写作'}{type:'add_todo', text:'睡觉', time:'晚上'}
reducer:一个普通的函数,用来修改store的状态。传入两个参数 state、action。其中,state为当前的状态(可通过store.getState()获得),而action为当前触发的行为(通过store.dispatch(action)调用触发)。reducer(state, action) 返回的值,就是store最新的state值。
// reducer方法, 传入的参数有两个// state: 当前的state// action: 当前触发的行为, {type: 'xx'}// 返回值: 新的statevar reducer = function(state, action){ switch (action.type) { case 'add_todo': return state.concat(action.text); default: return state; }};
关于actionAreator
看到xxCreator
总能让人联想到工厂模式,没错,在redux里,actionAreator其实就是action的工厂方法,可以参考下面例子。
actionCreator(args) => action
var addTodo = function(text){ return { type: 'add_todo', text: text };};addTodo('睡觉'); // 返回:{type: 'add_todo', text: '睡觉'}
在redux里,actionAreator并非是必需的。不过建议用actionAreator代替普通action对象的直接传递。除了能够减少代码量,还可以大大提高代码的可维护性。想象下面的场景再来看回文章开头的例子,应用actionAreator后的代码示例。
store.dispatch(addTodo('睡觉'));console.log('state is: ' + store.getState()); // state is: 读书,写作,睡觉
相关链接
redux中文文档:http://camsong.github.io/redux-in-chinese/index.html
redux英文文档:http://redux.js.org/index.html
redux源码解读:https://github.com/chyingp/redux-source-insight
- Redux系列01:从一个简单例子了解action、store、reducer
- 从一个简单例子了解action、store、reducer
- 从一个简单例子了解action、store、reducer
- 【redux】Action Reducer Store
- Redux系列01+核心概念 工作流程 安装 Action和Action创建函数 Reducer Store 数据流
- 认识redux的store,reducer,action
- Redux进阶系列3:如何设计action、reducer、selector
- Action Reducer Store
- 一个简单的例子让你了解React-Redux
- Redux系列02:一个炒鸡简单的react+redux例子
- 简化redux中的action和reducer
- redux 简单例子
- 通过一个demo了解Redux
- (二)Redux:实现一个Store
- redux(一)简单例子
- MapReduce 编程 系列三 编写简单reducer
- 一个例子了解apacheBench的简单使用方法
- 一个简单了解spring的例子
- 以ant方式从visual svn server checkout
- android studio配置butterknife 8.0以上版本
- HTML5学习笔记(零)
- 添加Action 按钮
- ssh三大框架整合--jar包
- Redux系列01:从一个简单例子了解action、store、reducer
- linux多线程学习(六)——信号量实现同步
- Construct Binary Tree from Inorder and Postorder Traversal
- jvm笔记4--类文件结构
- leetcode 35. Search Insert Position
- Filter高级开发(三)——压缩响应正文内容
- GL_CLAMP GL_CLAMP_TO_EDGE GL_CLAMP_TO_BORDER
- Caffe 初学拾遗(四) CUDA 框架说明
- JSON 对象与字符串相互转换