These examples are not completely compatible about whether to call next(action)
.
function incrementAsync() {
return dispatch => {
setTimeout(() => {
dispatch(increment());
}, 1000);
};
}
const INCREMENT_COUNTER_ASYNC = 'INCREMENT_COUNTER_ASYNC';
function incrementAsync() {
return {
type: INCREMENT_COUNTER_ASYNC
};
}
const incrementAsyncMiddleware = createMiddleware(
INCREMENT_COUNTER_ASYNC,
({ dispatch }) => {
setTimeout(() => {
dispatch(increment());
}, 1000)
}
);
function incrementIfOdd() {
return (dispatch, getState) => {
const { counter } = getState();
if (counter % 2 === 0) {
return;
}
dispatch(increment());
};
}
const INCREMENT_COUNTER_IF_ODD = 'INCREMENT_COUNTER_IF_ODD';
function incrementIfOdd() {
return {
type: INCREMENT_COUNTER_IF_ODD
};
}
const incrementIfOddMiddleware = createMiddleware(
INCREMENT_COUNTER_IF_ODD,
({ getState }) => {
const { counter } = getState();
return counter % 2 === 1;
},
({ dispatch }) => dispatch(increment())
);