其作用都是用来简化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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. React + Redux 入坑指南

    Redux 原理 1. 单一数据源 all states ==>Store 随着组件的复杂度上升(包括交互逻辑和业务逻辑),数据来源逐渐混乱,导致组件内部数据调用十分复杂,会产生数据冗余或者混用 ...

  4. [转] 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 ...

  5. 一个超级简单的demo带你走进redux的大坑

    先上代码 import React, { Component } from 'react'; import ReactDOM from 'react-dom'; import { createStor ...

  6. Redux 介绍

    本文主要是对 Redux 官方文档 的梳理以及自身对 Redux 的理解. 单页面应用的痛点 对于复杂的单页面应用,状态(state)管理非常重要.state 可能包括:服务端的响应数据.本地对响应数 ...

  7. react_结合 redux - 高阶函数 - 高阶组件 - 前端、后台项目打包运行

    Redux 独立的集中式状态管理 js 库 - 参见 My Git 不是 react 库,可以与 angular.vue 配合使用,通常和 react 用 yarn add redux import ...

  8. 一个Time TodoList实例了解redux在wepy中的使用

    @subject: wepy-redux-time-todo @author: leinov @date:2018-10-30 @notice: 小程序(wepy)开发群110647537 欢迎加入 ...

  9. react脚手架改造(react/react-router/redux/eslint/karam/immutable/es6/webpack/Redux DevTools)

    公司突然组织需要重新搭建一个基于node的论坛系统,前端采用react,上网找了一些脚手架,或多或少不能满足自己的需求,最终在基于YeoMan的react脚手架generator-react-webp ...

  10. 结合React使用Redux

    前面的两篇文章我们认识了 Redux 的相关知识以及解决了如何使用异步的action,基础知识已经介绍完毕,接下来,我们就可以在React中使用Redux了. 由于Redux只是一个状态管理工具,不针 ...

随机推荐

  1. gcc 学习

    gcc -lrt 参数说明:  说明在连接生成可执行文件的时候,将连接库librt.so or librt.a -l 参数说明 连接库 编译 使用 clock_gettime函数的,gcc需要添加上 ...

  2. Vuejs1.0学习

    1.数据双向绑定 双向绑定以后,表单中数据的改变会同步改变H2中的输出 2.v-show 内容输入前: 内容输入后:隐藏提示,展示按钮 代码实现: 此处的v-show可以换成v-if,v-show是隐 ...

  3. 手工安装kubernetes

    参考的URL是 http://www.cnblogs.com/zhenyuyaodidiao/p/6500830.html 安装kubernets本身比较顺利,只是作dashboard时,老是日文版, ...

  4. 关闭浏览器session就被干掉的假象的问题

    当在前台取出session时,关闭浏览器后再次访问服务器,这时服务器返回了一个null,此时的返回的session并非之前的那个session而是一个新的session. -->先来看看sess ...

  5. POJ 2932 Coneology(扫描线)

    [题目链接] http://poj.org/problem?id=2932 [题目大意] 给出N个两两没有公共点的圆,求所有不包含于其它圆内部的圆 [题解] 我们计算出所有点在圆心所有y位置的x值, ...

  6. 3.2常用类(java学习笔记)String与StringBuffer

    一.String String又称不可变字符序列. 我们看JDK源码中用于字符存储的数组有final修饰,final修饰变量就代表变量不能改变. 我们可以看API文档中对String的描述. Stri ...

  7. Java多线程——ReentrantLock源码阅读

    上一章<AQS源码阅读>讲了AQS框架,这次讲讲它的应用类(注意不是子类实现,待会细讲). ReentrantLock,顾名思义重入锁,但什么是重入,这个锁到底是怎样的,我们来看看类的注解 ...

  8. Java高级架构师(一)第11节:Mybatis的分页实现

  9. access日志配置

    链接地址:  https://wenku.baidu.com/view/3e20fac758f5f61fb73666cf.html org.apache.catalina.valves.AccessL ...

  10. JNI之数组

    Array Operations -- 数组操作 1.GetArrayLength jsize GetArrayLength(JNIEnv *env, jarray array); Returns t ...