Created
January 18, 2017 09:03
-
-
Save acanimal/786f95727f19ca469df1240c7fe143fd to your computer and use it in GitHub Desktop.
Higher Order Actions
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
const higherOrderAction = (func) => (...args) => (dispatch, ...props) => { | |
// Do whatever you need. | |
// Call the wrapped action. | |
const call = func(undefined, ...args); | |
// Check if the call returns a function, which means it is an async action | |
if (call && call.constructor && call.call && call.apply) { | |
return call(dispatch, ...props); | |
} | |
return dispatch(call); | |
}; | |
const actionStart = () => ({ | |
type: 'ACTION_ONE', | |
}); | |
const actionFinish = (result) => ({ | |
type: 'ACTION_ONE', | |
result, | |
}); | |
const actionAsync = (a, b) => (dispatch) => { | |
dispatch(actionStart()); | |
dispatch(actionFinish(a+b); | |
}; | |
export const higherActionStart = higherOrderAction(actionStart); | |
export const higherActionFinish = higherOrderAction(actionFinish); | |
export const higherActionAsync = higherOrderAction(actionAsync); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment