redux-actions
其作用都是用来简化action、reducer。
1.安装
npm install --save redux-actions
// 或
yarn add redux-actions
2.使用
createAction
原来创建action:
const action = { type: START };
使用redux-actions创建action:
import { createAction } from 'redux-actions';
const action = createAction(START);
// 注:
export const increment = createAction('INCREMENT')
export const decrement = createAction('DECREMENT')
// 等于:
increment() // { type: 'INCREMENT' }
decrement() // { type: 'DECREMENT' }
handleActions
原来的reducer
function timer(state = defaultState, action) {
switch (action.type) {
case START:
return { ...state, runStatus: true };
case STOP:
return { ...state, runStatus: false };
case RESET:
return { ...state, seconds: 0 };
case RUN_TIMER:
return { ...state, seconds: state.seconds + 1 };
default:
return state;
}
}
使用 redux-actions 操作 state
const timer = handleActions(
{
START: (state, action) => ({ ...state, runStatus: true }),
STOP: (state, action) => ({ ...state, runStatus: false }),
RESET: (state, action) => ({ ...state, seconds: 0 }),
RUN_TIMER: (state, action) => ({ ...state, seconds: state.seconds + 1 }),
},
defaultState
);
.
redux-actions的更多相关文章
- [React + Functional Programming ADT] Connect State ADT Based Redux Actions to a React Application
With our Redux implementation lousy with State ADT based reducers, it is time to hook it all up to a ...
- [Functional Programming ADT] Adapt Redux Actions/Reducers for Use with the State ADT
By using the State ADT to define how our application state transitions over time, we clear up the ne ...
- React + Redux 入坑指南
Redux 原理 1. 单一数据源 all states ==>Store 随着组件的复杂度上升(包括交互逻辑和业务逻辑),数据来源逐渐混乱,导致组件内部数据调用十分复杂,会产生数据冗余或者混用 ...
- [转] How to dispatch a Redux action with a timeout?
How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...
- 一个超级简单的demo带你走进redux的大坑
先上代码 import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { createStor ...
- Redux 介绍
本文主要是对 Redux 官方文档 的梳理以及自身对 Redux 的理解. 单页面应用的痛点 对于复杂的单页面应用,状态(state)管理非常重要.state 可能包括:服务端的响应数据.本地对响应数 ...
- react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行
Redux 独立的集中式状态管理 js 库 - 参见 My Git 不是 react 库,可以与 angular.vue 配合使用,通常和 react 用 yarn add redux import ...
- 一个Time TodoList实例了解redux在wepy中的使用
@subject: wepy-redux-time-todo @author: leinov @date:2018-10-30 @notice: 小程序(wepy)开发群110647537 欢迎加入 ...
- react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)
公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架generator-react-webp ...
- 结合React使用Redux
前面的两篇文章我们认识了 Redux 的相关知识以及解决了如何使用异步的action,基础知识已经介绍完毕,接下来,我们就可以在React中使用Redux了. 由于Redux只是一个状态管理工具,不针 ...
随机推荐
- mysql的expain(zz)
两张表,T1和T2,都只有一个字段,id int.各插入1000条记录,运行如下语句: explain SELECT t1.id,t2.id FROM t1 INNER JOIN t2 ON t1.i ...
- Java关于时间日期的Date类和Calendar类概述
1. System.currentTimeMillis()方法 可以获取当前时间距离1970年01月01日00时00分00秒的秒数,如果程序运行在北京时区,则获取的数据是当前时间距离1970 ...
- springCloud Zuul网关
1.springboot 仅2.0.x 支持,在此选择 2.0.7 2.新建Module eureka-zuul-client 3.导入依赖 <?xml version="1.0&qu ...
- Appium +Python 连接真机测试
1.数据线连接电脑和手机: 2.用adb获取手机的UUID:cmd-> adb devices 前面的就是你手机的UUID 3.打开appium,选择手机的安卓版本(关于手机中查看),填写手机的 ...
- HDU 多校1.10
- codeforces 713C C. Sonya and Problem Wihtout a Legend(dp)(将一个数组变成严格单增数组的最少步骤)
E. Sonya and Problem Wihtout a Legend time limit per test 5 seconds memory limit per test 256 megaby ...
- 机器人搬重物(BFS)
机器人搬重物 时间限制: 1 Sec 内存限制: 128 MB提交: 22 解决: 10[提交][状态][讨论版] 题目描述 机 器人移动学会(RMI)现在正尝试用机器人搬运物品.机器人的形状是一 ...
- create table select from
CREATE TABLE new_table AS SELECT * FROM old_table
- 【分块】bzoj3196 Tyvj 1730 二逼平衡树
分块 或 树套树. 在每个块中维护一个有序表,查询时各种二分,全都是分块的经典操作,就不详细说了. 块的大小定为sqrt(n*log2(n))比较快. #include<cstdio> # ...
- Inno Setup入门(十三)——Pascal脚本(2)
事件函数(2) function CheckPassword(Password: String): Boolean; 如果安装程序在Pascal 脚本中发现该函数,它自动显示密码页并调用CheckPa ...