Skip to content

Instantly share code, notes, and snippets.

@carlrip
Last active February 5, 2019 05:13
Show Gist options
  • Save carlrip/054620b1ad1ae6c6c0dad0e5ce71c652 to your computer and use it in GitHub Desktop.
Save carlrip/054620b1ad1ae6c6c0dad0e5ce71c652 to your computer and use it in GitHub Desktop.
React Redux Action Creators with TypeScript - example 2
export const postPersonActionCreator: ActionCreator<
ThunkAction<
Promise<IPostedPersonAction>, // The type of the last action to be dispatched - will always be promise<T> for async actions
IPostPersonResult, // The type for the data within the last action
IPostPerson, // The type of the parameter for the nested function
IPostedPersonAction // The type of the last action to be dispatched
>
> = (person: IPostPerson) => {
return async (dispatch: Dispatch) => {
const postingPersonAction: IPostingPersonAction = {
type: 'PostingPerson',
};
dispatch(postingPersonAction);
const result = await postPersonFromApi(person);
const postPersonAction: IPostedPersonAction = {
type: 'PostedPerson',
result,
};
return dispatch(postPersonAction);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment