博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[React] How to use a setState Updater Function with a Reducer Pattern
阅读量:5877 次
发布时间:2019-06-19

本文共 1112 字,大约阅读时间需要 3 分钟。

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 (      
{
this.state.value}
); }}export default App;

 

转载地址:http://lgdix.baihongyu.com/

你可能感兴趣的文章
L3.十一.匿名函数和map方法
查看>>
java面向对象高级分层实例_实体类
查看>>
android aapt 用法 -- ApkReader
查看>>
[翻译]用 Puppet 搭建易管理的服务器基础架构(3)
查看>>
Android -- AudioPlayer
查看>>
Python大数据依赖包安装
查看>>
Android View.onMeasure方法的理解
查看>>
Node.js 爬虫初探
查看>>
ABP理论学习之仓储
查看>>
NestJS 脑图
查看>>
我的友情链接
查看>>
Html body的滚动条禁止与启用
查看>>
Tengine新增nginx upstream模块的使用
查看>>
多媒体工具Mediainfo
查看>>
1-小程序
查看>>
CentOS图形界面和命令行切换
查看>>
HTML5通信机制与html5地理信息定位(gps)
查看>>
Mind_Manager_2
查看>>
手动升级 Confluence - 规划你的升级
查看>>
汽车常识全面介绍 - 悬挂系统
查看>>