Created
May 30, 2019 17:29
-
-
Save alexander-daniel/e62c84101cd75083f38d689a3adb30c4 to your computer and use it in GitHub Desktop.
This file contains 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
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