Redux thunk is one of the useful tool for redux based app. Just like redux logger (, that we discussed in our previous tutorial ), it is used as a middleware. The first time I tried to understand thunk I found it very confusing because in the beginning of its description it says “If you’re not sure whether you need it, you probably don’t”. Well if that’s the case, most developer won’t have used it. Most developer use a tool for development when they come to know the need and importance of the tool. Lets reanalyze this tool so that we can understand it and avail it’s usefulness.
Why Do We Need Redux Thunk?
We need redux thunk in a situation when we need to make a api call in our redux app and inject the data received from the api call to the redux store.
How Redux Thunk works?
Redux thunk is used in redux app as a middle-ware. When we use this middle-ware and write a action creator, the action creator returns a function instead of an action. The way we use this middle-ware is :
- make api call in action creator and pass result/data of call to action
- wrap the action to dispatch function
When we perform the above steps, the thunk middle-ware delays the dispatch of the action and the action creator returns a function instead of action.
The above explanation is still not very clear. To make things more clear please check out the video below :