Last active
April 22, 2018 10:29
-
-
Save dewey92/dae041962972847efee3ba3633e2dae6 to your computer and use it in GitHub Desktop.
Async Action Creators
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 createAction = actionName => Object.assign( | |
| payload => ({ | |
| type: actionName, | |
| payload | |
| }), | |
| { type: actionName } // <- Inject `type` attribute to the action func | |
| ) | |
| const createAsyncAction = actionName => ({ | |
| request: createAction(actionName + '_REQUEST'), | |
| success: createAction(actionName + '_SUCCESS'), | |
| failed: createAction(actionName + '_FAILED'), | |
| }) | |
| const getUser = createAsyncAction('GET_USER') | |
| const getPost = createAsyncAction('GET_POST') | |
| // Contoh cara pemakaiannya: | |
| // | |
| // getPost.request(99) | |
| // getPost.success({ id: 99, title: 'Aku ganteng' }) | |
| // getPost.failed({ error: 'Maaf, Anda kurang ganteng' }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment