Created
July 21, 2018 04:00
-
-
Save iinsta/0fbb2574dc262bc5b551dde5c4e48355 to your computer and use it in GitHub Desktop.
better redux thunk
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getNewState(getState, isOriginal) { | |
const state = getState(); | |
return isOriginal ? state : object.assign({}, state); | |
} | |
function createThunkMiddleware(extraArgument, errorHandler) { | |
return ({ dispatch, getState }) => next => action => { | |
if (typeof action === 'function') { | |
const result = action(dispatch, (isOriginal) => getNewState(getState, isOriginal), extraArgument); | |
if (errorHandler && result.catch) result.catch(errorHandler); | |
return result; | |
} | |
return next(action); | |
}; | |
} | |
const thunk = createThunkMiddleware(); | |
thunk.withExtraArgument = createThunkMiddleware; | |
export default thunk; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment