Created
March 4, 2021 07:11
-
-
Save atharva-bhange/6e181e639a4d8016a7ca9fcf8ddd2ae9 to your computer and use it in GitHub Desktop.
Calling Async action creators without using thunk or saga
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
// 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 ); |
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
// 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