What Is Redux Thunk?
Redux Thunk is a middleware for Redux that allows you to write action creators that return a function instead of a plain action object.
Normally, Redux actions look like this:
{ type : " SET_USER " , payload : user }
Enter fullscreen mode
Exit fullscreen mode
async code doesn't work directly: Redux would throw an error because it expects an object.
dispatch ( async () => { const data = await api . getUser () })
Enter fullscreen mode
Exit fullscreen mode
But with thunk, you can return a function:
const fetchUser = () => { // Return an async function that receives dispatch as parameter return async ( dispatch ) => { const response = await api . getUser ()
dispatch ({ type : " SET_USER " , payload : response .…