Skip to content

Instantly share code, notes, and snippets.

@iinsta
Created July 21, 2018 04:00
Show Gist options
  • Save iinsta/0fbb2574dc262bc5b551dde5c4e48355 to your computer and use it in GitHub Desktop.
Save iinsta/0fbb2574dc262bc5b551dde5c4e48355 to your computer and use it in GitHub Desktop.
better redux thunk
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