Skip to content

Instantly share code, notes, and snippets.

@carlrip
Last active February 5, 2019 05:13
Show Gist options
  • Save carlrip/8836b6704a77672141bd68b4a990fab7 to your computer and use it in GitHub Desktop.
Save carlrip/8836b6704a77672141bd68b4a990fab7 to your computer and use it in GitHub Desktop.
React Redux Action Creators with TypeScript - example 1
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