Skip to Content
React学习手册
book

React学习手册

by Alex Banks, Eve Porcello
December 2017
Intermediate to advanced
329 pages
6h 9m
Chinese
China Electric Power Press Ltd.
Content preview from React学习手册
194
8
消除
Reducer
的副作用
Reducer
应该是可预见的。它们只是用来管理
State
数据的。在上一个示例中需
要注意,时间戳和
ID
是在将
Action
发送给
Redcuer
之前生成的。生成随机数据,
调用
API
,以及其他异步过程应该在
Reducer
之外处理。为了避免
State
变异和产
生副作用,所以才推荐用户这么做。
我们还可以从
State
中移除单个颜色,或者通过将相应的
Action
发送给
colors
Reducer
来对单个颜色进行评分。
Sort Reducer
sort
Reducer
是一个完整的函数,用于管理
State
中某个字符串变量:
export const sort = (state = "SORTED_BY_DATE", action) => {
switch (action.type) {
case C.SORT_COLORS:
return action.sortBy
default :
return state
}
}
sort
Reducer
是用来修改
sort
函数中的
State
参数的值。它会将
State
参数值设置为
Action
sortBy
字段中的值(如果没有提供
State
参数,它将返回
SOR TED_BY_DATE
):
const state = "SORTED_BY_DATE"
const action = {
type: C.SORT_COLORS,
sortBy: "SORTED_BY_TITLE" ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

React快速上手开发

React快速上手开发

Stoyan Stefanov
流畅的Python

流畅的Python

Luciano Ramalho
C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普

Publisher Resources

ISBN: 9787519814236