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. hdu 1002(大数)

    A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. SSH整合报错:找不到元素 'beans' 的声明

    转自:https://blog.csdn.net/haozhugogo/article/details/54233608 spring版本问题,将bean.xml中xsd文件定义的版本改为spring ...

  3. Promise解决回调地狱

    Promise是JavaScript异步操作解决方案.介绍Promise之前,先对异步操作做一个详细介绍. JavaScript的异步执行 概述 Javascript语言的执行环境是”单线程”(sin ...

  4. golang iris下面的websocket

    最近要做后台主动推送:(iris框架,封装的有wesocket,刚开始以为直接拿过来用,结果不是现在贴一下代码,写一下遇到的坑) func main() {    app := iris.New()  ...

  5. mysql.connector 事务总结

    mysql.connector事务总结: connection.autocommit = 0 (默认值) 事务处理 使用 connection.commit()方法 #!/usr/bin/env py ...

  6. C# 常用代码片段

    一.从控制台读取东西代码片断: using System; class TestReadConsole { public static void Main() { Console.Write(Ente ...

  7. Android studio 隐藏toolbar上的app title

    getSupportActionBar().setDisplayShowTitleEnabled(false);

  8. 使用eclipse,对spring boot 进行springloader或者devtool热部署失败处理方法

    确定配置进行依赖和配置没有错误后. 调整spring boot 的版本,因为新版本对老版本的spring boot 不能使用. 改为: <groupId>org.springframewo ...

  9. testdirector

    TestDirector是全球最大的软件测试工具提供商Mercury Interactive公司生产的企业级测试管理工具,也是业界第一个基于Web的测试管理系统

  10. bzoj 4372: 烁烁的游戏 动态点分治_树链剖分_线段树

    [Submit][Status][Discuss] Description 背景:烁烁很喜欢爬树,这吓坏了树上的皮皮鼠. 题意: 给定一颗n个节点的树,边权均为1,初始树上没有皮皮鼠. 烁烁他每次会跳 ...