action只是描述了“发生了什么事情(导致state需要更新)”,但并不直接参与更新state的操作。state的更新由reducer函数执行。

其基本模式是:(state,action)=> newState

设计组件state的结构:

    在开始敲代码之前,我们要先设计好组件state的结构。我们得先明确state需要哪些变量,哪些是纯粹的数据,哪些和UI有关。用合适的结构组织起来(data类的state和UI类的state最好相互独立)。

In a more complex app, you're going to want different entities to reference each other. We suggest that you keep your state as normalized as possible, without any nesting. Keep every entity in an object stored with an ID as a key, and use IDs to reference it from other entities, or lists. Think of the app's state as a database.

如何处理action:

往reducer函数传入参数之后,根据action的type属性,函数执行对应的对state的更新。文档指出三件在reducer函数中必须避免的事情:

1.改变传入的参数。

2.执行“引发其他作用“的操作,比如调用其他api、路由跳转

3.调用”无更新无关“的函数,如Date.now()

For now, just remember that the reducer must be pure. Given the same arguments, it should calculate the next state and return it. No surprises. No side effects. No API calls. No mutations. Just a calculation.

使用

初始化state:

在第一次调用reducer的时候,传入的state回事一个undefined,此时我们可以给它进行初始化。

 import { VisibilityFilters } from './actions'

 const initialState = {
visibilityFilter: VisibilityFilters.SHOW_ALL,
todos: []
} function todoApp(state, action) {
if (typeof state === 'undefined') {
return initialState
} // For now, don't handle any actions
// and just return the state given to us.
return state
}

我们可以借助ES6的参数默认值简化代码:

 function todoApp(state = initialState, action) {
// For now, don't handle any actions
// and just return the state given to us.
return state
}

最终的reducer可以是这样:

 function todoApp(state = initialState, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return Object.assign({}, state, {
visibilityFilter: action.filter
})
case ADD_TODO:
return Object.assign({}, state, {
todos: [
...state.todos,
{
text: action.text,
completed: false
}
]
})
default:
return state
}
}

记住几个地方:

*只要符合switch语句的某个case,返回的state是一个新的对象。

*匹配不到case的,default返回的是原先的state。任何意外的action都不触发state更新,可控才是最重要的。

*Object.assign是ES6的api,许多浏览器可能并不原生支持,开发时记得安装插件 Babel plugin。另,assign第一个参数必须是空对象;也可以用 对象扩展运算符{...state,...update}。

*switch语句不是必须的,reducer内部的结构完全由自己设计

分解(解构)Reducer:

当我们的state涉及很多变量的时候,像上面那样写在同一个函数中难免会显得又长又臭。

作者给出的优化方法是,在Reducer中直接返回一个新的字面量的state。但是这个state各个属性的值是一个reducer(state.key,action)的返回值。

也就是说,返回的state的各个属性,都经过一个reducer的处理,如果该属性的值需要更新。则内部返回一个新属性值。如果该属性不需要更新,则内部返回原来的属性值。

最后store获得的是响应了action的更新了的state。

看下面的例子:

 function todos(state = [], action) {
switch (action.type) {
case ADD_TODO:
return [
...state,
{
text: action.text,
completed: false
}
]
case TOGGLE_TODO:
return state.map((todo, index) => {
if (index === action.index) {
return Object.assign({}, todo, {
completed: !todo.completed
})
}
return todo
})
default:
return state
}
} function visibilityFilter(state = SHOW_ALL, action) {
switch (action.type) {
case SET_VISIBILITY_FILTER:
return action.filter
default:
return state
}
} function todoApp(state = {}, action) {
return {
visibilityFilter: visibilityFilter(state.visibilityFilter, action),
todos: todos(state.todos, action)
}
}

这段代码结构很清晰,todos函数负责管理事件的添加和‘是否完成’的标记,visibilityFilter负责列表的显示规则,顶层Reducer返回的state的各个属性的值都是一个reducer的返回值。由于内部reducer的函数名和state的属性名一致,所以可以用对象属性简写的方式:return { visibilityFilter,todos};

要留意

内部reducer单元中,参数state都有默认值,因为每个reducer分别更新state的不同属性,因此他们得到的state参数其实是state的一部分属性,他们返回的值也是新state的一部分。

顶层Reducer才是返回新state的地方。

另外,Redux提供了一个工具 combineReducers()来帮助我们实现上面这种分解reducer功能的逻辑:

 import { combineReducers } from 'redux'

 const todoApp = combineReducers({
visibilityFilter,
todos
}) export default todoApp 等价于 export default function todoApp(state = {}, action) {
return {
visibilityFilter: visibilityFilter(state.visibilityFilter, action),
todos: todos(state.todos, action)
}
}

就是一个生成Reducer的工具。

Redux:Reducers的更多相关文章

  1. [React Testing] Redux Reducers

    Sometimes we want to test our Redux reducers to make sure they work as expected. In this lesson we w ...

  2. [Redux + Webpack] Hot reloading Redux Reducers with Webpack

    Webpack will hot reload the component, but the reducer we need hard refresh. To sovle the problem, g ...

  3. [Functional Programming ADT] Combine Multiple State ADT Based Redux Reducers

    Redux provides a convenient helper for combining many reducers called combineReducer, but it focuses ...

  4. Redux学习笔记:Redux简易开发步骤

    该文章不介绍Redux基础,也不解释各种乱乱的概念,网上一搜一大堆.只讲使用Redux开发一个功能的步骤,希望可以类我的小白们,拜托它众多概念的毒害,大牛请绕道! 本文实例源代码参考:React-Re ...

  5. react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行

    Redux 独立的集中式状态管理 js 库 - 参见 My Git 不是 react 库,可以与 angular.vue 配合使用,通常和 react 用 yarn add redux import ...

  6. react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)

    公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架generator-react-webp ...

  7. redux详解

    redux介绍 学习文档:英文文档,中文文档,Github redux是什么 redux是一个独立专门用于做状态管理的JS库(不是react插件库),它可以用在react, angular, vue等 ...

  8. 3.1 开始使用 redux

    前面我们介绍了 flux 架构以及其开源实现 redux,在这一节中,我们将完整的介绍 redux: redux 介绍 redux 是什么 redux 概念 redux 三原则 redux Store ...

  9. Redux(mvc、flux、react-redux)

    其他章节请看: react实战 系列 Redux 关于状态管理,在 Vue 中我们已经使用过 Vuex,在 spug 项目中我们使用了 mobx,接下来我们学习 Redux. 本篇以较为易懂的方式讲解 ...

随机推荐

  1. IDE使用GIT控制项目版本

    IDEA本身继承GIT开发插件.只需要安装windows git客户端即可使用. check in project 检入项目 将新创建的项目上传到服务器. 对于git来说,空的目录不会上传到远程仓库. ...

  2. failed to open stream :HTTP request failed 解决方法

    用curl抓取,不要用file_get_contents(); 前者比后者效率高一点

  3. Selenium常见报错问题(2)- 解决和分析StaleElementReferenceException异常

    如果你在跑selenium脚本时,需要某些异常不知道怎么解决时,可以看看这一系列的文章,看看有没有你需要的答案 https://www.cnblogs.com/poloyy/category/1749 ...

  4. 监控CPU与GPU的工具

    1.sensor:可以显示包括cpu在内的所有传感器的当前读数 使用sensors可以检测到cpu的温度,风扇的风速度,电压等. 2.Glances使用Python写的跨平台的curses的检测工具. ...

  5. js 随机数生成器

    title: js 随机数生成器 js 随机数生成器 js 随机数生成器 确定产生随机数的数目,最小值和最大值: 个数: 最小值: 最大值: 是否为唯一的随机数: 唯一 允许重复 点击生成产生随机数: ...

  6. Shell中的here文档

    1.名词解释: 以下是维基百科解释: here文档[1],又称作heredoc.hereis.here-字串或here-脚本,是一种在命令行shell(如sh.csh.ksh.bash.PowerSh ...

  7. Android xUtils3.0使用手册(二) - 数据库操作

    步骤:  (1). 创建数据表: (2). DaoConfig 获取数据库的配置信息: (3).  获取数据库实例:  x.getDb(daoConfig); (4). 数据库的增删改查. 1. 创建 ...

  8. java1-3总结 19201421-吴志越

    关于最近几次作业,从C语言到Java的过渡,也就是从面向过程到面向对象的过渡.其中,一共有三次作业,前俩次可能更加偏向于过程的设计,利用C语言的想法就可以完成,但是,从需要使用类的开始,就逐渐向对象偏 ...

  9. 【Linux常见命令】tar命令

    [独立命令,只能用其中一个,和别的命令连用]这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个. -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末 ...

  10. 打造更好用的 EF 自动审计

    打造更好用的 EF 自动审计 Intro 上次基于 EF Core 实现了一个自动审计的功能,详细可以参考 https://www.cnblogs.com/weihanli/p/auto-audit- ...