In this lesson we'll walk through setting up an updater function that can receive an action argument. We'll also dive into how to separate your state management logic into a separate reducer function much like how Redux operates. It will receive an action which we can add any data and update state accordingly.

import React, { Component } from "react";

const INCREMENT = "INCREMENT";
const DECREMENT = "DECREMENT"; const reducer = action => (state, props) => {
switch (action.type) {
case INCREMENT:
return {
value: state.value + action.amount,
};
case DECREMENT:
return {
value: state.value - 1,
};
default:
return null;
}
}; class App extends Component {
state = {
value: 0,
};
increment = () => {
this.setState(
reducer({
type: INCREMENT,
amount: 2
}),
);
};
decrement = () => {
this.setState(
reducer({
type: DECREMENT,
}),
);
};
render() {
return (
<div>
<div>{this.state.value}</div>
<button onClick={this.increment}>Increment</button>
<button onClick={this.decrement}>Decrement</button>
</div>
);
}
} export default App;

[React] How to use a setState Updater Function with a Reducer Pattern的更多相关文章

  1. React源码解析:setState

    先来几个例子热热身: ......... constructor(props){ super(props); this.state = { index: 0 } } componentDidMount ...

  2. React与Preact差异之 -- setState

    Preact是React的轻量级实现,是React比较好的替代者之一,有着体积小的优点,当然与React之间一定会存在实现上的差异,本文介绍了在 setState 方面的差异之处. 源码分析 首先来分 ...

  3. react native 打包Ignoring return value of function declared with warn_unused_result attribute

    从 github上下载 项目 用于学习查看别人的代码, 当执行完npm install  用xcode 打开 发现俩个错误提示Ignoring return value of function dec ...

  4. (文章也有问题,请自行跳过)react中的状态机每次setState都是重新创建新的对象,如需取值,应该在render中处理。

    demo如下 class Demo4StateLearn extends React.Component { constructor(props) { super(props); this.state ...

  5. JavaScript Patterns 4.8 Function Properties - A Memoization Pattern

    Gets a length property containing the number of arguments the function expects: function func(a, b, ...

  6. type Props={};

    Components Learn how to type React class components and stateless functional components with Flow Se ...

  7. react 源码之setState

    今天看了react源码,仅以记录. 1:monorepo (react 的代码管理方式) 与multirepo 相对. monorepo是单代码仓库, 是把所有相关项目都集中在一个代码仓库中,每个mo ...

  8. 从 0 到 1 实现 React 系列 —— 4.setState优化和ref的实现

    看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/生命周期/diff算法/setState/ref/. ...

  9. React的setState分析

    前端框架层出不穷,不过万变不离其宗,就是从MVC过渡到MVVM.从数据映射到DOM,angular中用的是watcher对象,vue是观察者模式,react就是state了. React通过管理状态实 ...

随机推荐

  1. Sinowal Bootkit 分析-中国红客网络技术联盟 - Powered by Discuz!

    訪问原文 (一)模块组成         感染过Sinowal的电脑,Sinaowal在硬盘中的分布例如以下图: ; Sector                 Offset             ...

  2. JavaScript:DOM对象

    ylbtech-JavaScript:DOM对象 1. HTML DOM Document 对象返回顶部 1. HTML DOM Document 对象 HTML DOM 节点 在 HTML DOM ...

  3. Linux Shell Scripting Cookbook 读书笔记 4

    正则, grep 1. 正则表达式  正则表达式  描述  示例 ^ 行起始标记  ^hell匹配以hell开头的行 $ 行尾标记  test$匹配以test结尾的行 . 匹配任意一个字符  hell ...

  4. 了解php数据转json格式与前端交互基础

    php数据转json格式与前端交互 ArryJson1.php <?php $test=array(); $word=array("我12","要43", ...

  5. Python 生成requirement 使用requirements.txt

    第一步:python项目中必须包含一个 requirements.txt 文件,用于记录所有依赖包及其精确的版本号.以便新环境部署. requirements.txt可以通过pip命令自动生成和安装 ...

  6. Codeforces Round #455

    Generate Login 第二个单词肯定只取首字母 Solution Segments 从1开始的线段和在n结束的线段各自凑一凑,剩下的转化为规模为n-2的子问题. Solution Python ...

  7. JavaScript实现数字时钟功能

    <html> <head> <meta charset="utf-8"> <title>无标题文档</title> &l ...

  8. DELPHI调试出现disconnected session的解决办法

    我在控制面板中,是禁用了UAC的,如下图 但是,在注册表中启用了UAC(EnableLUA), 工程中请求了管理员权限,如下图: 所以,整个权限请求混乱了. 解决办法,要么把注册表的LUA设置为0,要 ...

  9. AI:**消灭程序员需要一百年吗?

    这篇博文真的很长,不过挺有意思.关于智能机器人的发展前景还是很广的,因为每一步都异常艰难,而什么时候可以终止还无法预料,所以程序员没办法失业啊! 转自于图灵社区:http://www.ituring. ...

  10. Python:Matplotlib 画曲线和柱状图(Code)

    原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 参考资料:http://matplotlib.org/gallery.html ...