Answer the question
In order to leave comments, you need to log in
What is Redux-thunk for?
What is redux-thunk for if you can work like this:
import {START, SUCCESS, FAIL} from '../constants'
export default store => next => action => {
const {callAPI, type, ...rest} = action
if (!callAPI) return next(action)
next({
...rest, type: type + START
})
fetch(callAPI)
.then(res => res.json())
.then(response => next({...rest, type: type + SUCCESS, response}))
.catch(error => next({...rest, type: type + FAIL, error}))
}
Answer the question
In order to leave comments, you need to log in
to be honest, a strange question - the task is trivial, but in order not to scare off the masses, a well-tested solution was written, which is enough for a lot of cases
function createThunkMiddleware(extraArgument) {
return ({ dispatch, getState }) => next => action => {
if (typeof action === 'function') {
return action(dispatch, getState, extraArgument);
}
return next(action);
};
}
const thunk = createThunkMiddleware();
thunk.withExtraArgument = createThunkMiddleware;
export default thunk;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question