在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ Redux中store.dispatch()的參數(shù)為函數(shù)?

Redux中store.dispatch()的參數(shù)為函數(shù)?

在Redux官方示例shopping-cart中

//src/actions/index.js
const receiveProducts = products => ({
    type: types.RECEIVE_PRODUCTS,
    products: products
})
export const getAllProducts = () => dispatch => {
    shop.getProducts(products => {
        dispatch(receiveProducts(products))
    })
}
//src/index.js
import { getAllProducts } from './actions'
//省略
//console.log(getAllProducts());
//在chrome中打印如下
//function (dispatch) {
//        __WEBPACK_IMPORTED_MODULE_0__api_shop__["a" /* default //*/].getProducts(function (products) {
//            dispatch(receiveProducts(products));
//        });
//    }
//
store.dispatch(getAllProducts())

store.dispatch()的參數(shù)應(yīng)該是action吧,但是這里的getAllProducts()執(zhí)行結(jié)果是一個函數(shù)聲明。

回答
編輯回答
疚幼

store.dispatch()是 View 發(fā)出 Action 的唯一方法。View 要發(fā)送多少種消息,就會有多少種 Action。如果都一一手寫,會很麻煩。可以定義一個函數(shù)來生成 Action,這個函數(shù)就叫 Action Creator。上面代碼中,getAllProducts函數(shù)就是一個 Action Creator。

2018年6月11日 17:35
編輯回答
別傷我

因為action也可以是一個函數(shù),比如異步action當(dāng)中用到的redux-thunk這個中間件,本質(zhì)上就是檢測為函數(shù)類型的action。

2018年2月11日 22:54