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