一、安装

cnpm install --save redux-actions

二、为什么使用 redux-actions

reducer使用switch case语句进行action类型判断,当action很多时候,reducer内容就不那么直观了。redux-actions简化了reducer和action的联系

三、基本使用

1、创建action/actionCreator.js

import { createAction } from 'redux-actions';
export const addnum = createAction('ADDNUM')

2、组件中引入使用

import React,{Component} from "react";
import store from "./store"
import {addnum} from "./action/actionCreator"
export default class App extends Component{
  constructor(){
    super()
    this.state = store.getState();
    store.subscribe(this.handleUpdate.bind(this))
  }
  render(){
    let {n} = this.state;
    return (
      <div>
        <h2>{n}</h2>
        <button onClick={this.handleClick.bind(this)}>点击</button>
      </div>
    )
  }
  handleClick(){
    store.dispatch(addnum());
  }
  handleUpdate(){
    this.setState(store.getState())
  }
}

3、reducer中使用

import {handleActions} from 'redux-actions';
const defaultState = {
n:10
} export default handleActions({
ADDNUM: (state, action) => {
let newState = {...state};
newState.n++;
return newState;
},
}, defaultState)

最新文章

  1. 透明ActionBar
  2. 在ESXi 5.x 和 ESXi 6.0.x 中如何安装第三方供应商开发的驱动程序
  3. zookeeper适用场景:配置文件同步
  4. ORA-00845: MEMORY_TARGET not supported on this system
  5. 初学python(print使用、条件分支、循环、模块引用)
  6. mac下的改装人生——制作mac os 启动盘
  7. tp5 隐藏index.php 邓士鹏
  8. Android 自定义类型文件与程序关联
  9. SpringBoot-@RequestParam
  10. hdu3037 lucas
  11. Bootstrap中模态框多层嵌套时滚动条问题
  12. Android购物车的实现,仿淘宝天猫京东等APP。处理RecyclerView或listview中的选中事件;
  13. 20165315 实验一 Java开发环境的熟悉
  14. C语言中使用的地址是真实的物理地址吗?
  15. LintCode: Unique Characters
  16. [THUSC 2016] 补退选 (Trie树)
  17. 视音频数据处理入门:H.264视频码流解析
  18. select的placeholder和分组效果
  19. Java 类设计----Java类的继承
  20. Autofac Mvc注入

热门文章

  1. yii之relations关联非主键
  2. java知识点拾遗:)
  3. How many groups(DP)
  4. SSH远程免密码的密钥登录服务(Linux,Linux)
  5. A Bite Of React(1)
  6. go 复合数据类型
  7. vue证明题X,vue设置集
  8. 批量授一种权限给用户程序 plm enovia
  9. 编译lineageos1
  10. 十、设计模式之代理(Proxy)模式