redux-actions的api不多,有三個createAction(s) handleASction(s) combineActionsnpm
主要用到createAction去統一管理action,雖然會增長一些代碼量,但能夠統一管理action,對代碼維護有很大方便。redux
項目是用的dva框架,這個跟框架無關,createAction完成的是執行了一個dispatchapi
使用以前:框架
dispatch({type:'products/asyncDecr',payload:1})
payload能夠傳遞參數async
使用以後:this
increment()
能夠給方法increment({a:1,b:2})傳入參數,參數會在加載到payload參數上,能夠直接取出來使用。spa
具體用法:code
安裝:$ npm install redux-actions --savecomponent
使用:blog
新建action目錄下index.js文件:
import { createAction } from 'redux-actions'; export const increment = createAction('products/increment'); export const decrement = createAction('products/decrement'); export const asyncDecr = createAction('products/asyncDecr');
UI component中使用:
首先引入組件:
import { increment, asyncDecr } from '../actions';
利用connect將actions鏈接到組件:
export default connect(mapStateToProps, { increment, asyncDecr })(ProductPage);
取出使用:
const { products, dispatch, increment, asyncDecr } = this.props;
<Button type="primary" onClick={()=>increment()}>increment</Button> <Button type="primary" onClick={()=>asyncDecr()}>asyncDecr</Button>