Skip to content

Instantly share code, notes, and snippets.

@atharva-bhange
Created March 4, 2021 07:11
Show Gist options
  • Save atharva-bhange/6e181e639a4d8016a7ca9fcf8ddd2ae9 to your computer and use it in GitHub Desktop.
Save atharva-bhange/6e181e639a4d8016a7ca9fcf8ddd2ae9 to your computer and use it in GitHub Desktop.
Calling Async action creators without using thunk or saga
// Wrapped up into a funciton in case you need to re-use it other places
export function getAllPosts( dispatch: Dispatch<getPostsAction> ): () => void {
return () => {
console.log("runnning");
// try {
// console.log("lol");
// const response = await axios.get("/posts");
// switch (response.status) {
// case 200:
// console.log(response.data);
// dispatch({
// type: "GET_POSTS",
// payload: [],
// });
// }
// } catch (err) {
// console.log(err);
// }
dispatch({ type: "GET_POSTS", payload: [] });
};
}
function mapAppDispatchToProps( dispatch: Dispatch<getPostsAction> ): Pick<AppProps, 'getPosts'> {
return{getPosts: getAllPosts( dispatch )}
}
const ConnectedApp = connect( mapStateToProps, mapAppDispatchToProps )( App );
// Calling a async action without using thunk or saga in the component itself
function mapDispatchToProps(dispatch: Dispatch<MyAction>): Pick<Props,'getPosts'> {
return: {
getPosts: async () => {return dispatch( getAction( await fetch('/stuff') ) );},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment