在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()的参数应该是action吧,但是这里的getAllProducts()执行结果是一个函数声明。
因为action也可以是一个函数,比如异步action当中用到的redux-thunk这个中间件,本质上就是检测为函数类型的action。