Last active
February 5, 2019 05:13
-
-
Save carlrip/8836b6704a77672141bd68b4a990fab7 to your computer and use it in GitHub Desktop.
React Redux Action Creators with TypeScript - example 1
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
export const getPeopleActionCreator: ActionCreator< | |
ThunkAction< | |
Promise<IGotPeopleAction>, // The type of the last action to be dispatched - will always be promise<T> for async actions | |
IPerson[], // The type for the data within the last action | |
null, // The type of the parameter for the nested function | |
IGotPeopleAction // The type of the last action to be dispatched | |
> | |
> = () => { | |
return async (dispatch: Dispatch) => { | |
const gettingPeopleAction: IGettingPeopleAction = { | |
type: 'GettingPeople', | |
}; | |
dispatch(gettingPeopleAction); | |
const people = await getPeopleFromApi(); | |
const gotPeopleAction: IGotPeopleAction = { | |
people, | |
type: 'GotPeople', | |
}; | |
return dispatch(gotPeopleAction); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment