Action創(chuàng)建函數(shù)就是創(chuàng)建action對象的函數(shù)蛆楞,起初它只能返回action對象溯乒,但通過中間件的加工后,action創(chuàng)建函數(shù)可以返回更多的類型豹爹。Redux Thunk中間件就是去加工action創(chuàng)建函數(shù)裆悄。
- Action創(chuàng)建函數(shù)
function increment(){
return {type: 'INCREMENT'};
}
發(fā)起:store.dispatch(increment()); - Redux Thunk中間件(需安裝react-thunk包)
function incrementIfOdd(){
return (dispatch, getState){
const value = getState();
if(value % 2 ===0){
return;
}
dispatch(increment());
};
}
激活:const store = createStore(counter, applyMiddleware(thunk));
store.dispatch(incrementIfOdd());