Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexander-daniel/e62c84101cd75083f38d689a3adb30c4 to your computer and use it in GitHub Desktop.
Save alexander-daniel/e62c84101cd75083f38d689a3adb30c4 to your computer and use it in GitHub Desktop.
function updatePosts(posts) {
return {
type: "UPDATE_POSTS",
posts
};
}
// action thunk
function goGetLatestPostsForUser(userID) {
return async (dispatch, getState) => {
// make normal http request to our API backend
const response = fetch('/getPosts/${useriD}');
const data = response.body // the shape of the data here is determined by whatever the API returns, might be a JSOn object, might be a string, could be boolean, whatever. This example pretends it's ready to be stored right away
dispatch(updatePosts(data))
};
}
// usage
const mapDispatchToProps = (dispatch, ownProps) => {
return {
getPosts: async () => {
dispatch(setLoading(true));
await dispatch(goGetLatestPostsForUser(ownProps.userID));
dispatch(setLoading(false));
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment