Redux is a state management JavaScript library that can be extremely useful in React and React Native applications. In an application, keeping track of where state and props live and are passed to can quickly get confusing as your application grows. Redux helps simplify this process by storing the application’s state globally in a central store that is then manipulated by components through dispatching actions that get passed to a reducer. The reducer takes an action and updates the central store (application’s state) synchronously. This works great until you need to perform asynchronous actions, such as network requests. In this case, the common way to dispatch asynchronous actions is by using Redux Thunk middleware, which works by having the action creators return a function instead of an action object.
Setup
The setup is relatively simple:
npm install redux-thunk
Then add Thunk to your store file. It should look something like this (uses Redux’s applyMiddleware()):
Actions Examples
Normal Action — returns action object
Asynch Action — returns function
Like the above example, once the asynchronous function is called, you can return to the normal flow of synchronous actions (dispatching the object to a reducer) or can dispatch another action (or actions).