This video is available to students only

Middlewares

Middleware allows for side-effects to be executed without blocking state updates. Logger and Analytics make for perfect use cases.

Middleware allows for side-effects to be executed without blocking state updates. We can run side-effects (like API calls) in response to a specific action, or in response to every action that is dispatched (like logging, etc.).

It provides a third-party extension point between dispatching an action, and the moment it reaches the reducer.

Redux Thunk#

With the basic Redux store, we can only do synchronous updates by dispatching an action. Thunk is a middleware that extends the store's abilities and lets us write async logic that can get data from and set data to the store.

Redux Thunk is the official version of the async function middleware for Redux. It allows us to write action creators that return a function instead of an action.

To use Thunk, we need to initialize it when creating the application store:

Redux Thunk gets dispatch and getState as arguments.

Thunks are the best place to make API calls, add any business logic, or handle errors and responses before passing them to reducers for updating store and consumption on UI.

Start a new discussion. All notification go to the author.