Redux-Thunk
one minute of reading
Redux-Thunk is a library that allows you to perform asynchronous operations in a Redux-based application. It is one of the most popular Redux extensions that allows you to easily and clearly manage the state of your application.
Redux-Thunk is an add-on to the Redux library. Hosted as a separate package in NPM, it can be downloaded and set up as middleware when initializing Redux without complex configuration. It allows you to achieve asynchronous querying. Middleware in redux allows you to intercept a query and instead of making a single change on the stor it can make a series of queries, in the meantime they can query the API for external data. The whole thing is possible, obtainable without Redux-Thunk, but this one makes it much easier.
An example query in Redux-Thunk
The simplest example of using Redux-Thunk would be to query the API to log in a user. For the query to run correctly, a couple of things need to happen.
- Setting the application state that the login has started.
- Sending the query to the API
- Setting the user data in the store
- Possibly returning an error
export const authLogin = (email, password) => {
return dispatch => {
dispatch(authStart());
axios
.post(`${API_URL}/auth/api/v1/login/`, {
email: email,
password: password
})
.then(res => {
const token = res.data.token;
const user = res.data.user;
dispatch(authSuccess(token, user));
})
.catch(err => {
dispatch(authFail(err));
});
};
};
With this approach, we can isolate some of the logic of our application to a single function. We can use data mocking from the API and fully test the performance of such a Thunk.
Alternatives to Redux-thunk
An alternative to Redux-thunk is the Context API, which is built directly into the React library. We can easily create asynchronous queries there. We will only see a practical difference when the Context API supports a sizable object where we will frequently update parameters. We may find that the Context API is then less efficient and runs DOM updates too often.
Although Redux is a separate library it has rich documentation with well-described examples. Examples for Redux-Thunk are also numerous and it is definitely a productive solution for maintaining application state.
Related articles
Why Tailwind UI is a Must-Know for Modern Web Developers?
26 Oct 2023
In the realm of modern web development, the need for efficient tools is increasingly exigent. Harnessing the power of such an asset, Tailwind UI is emerging as a comprehensive solution. Streamlining the development process, it allows to make compelling web interfaces with ease. This write-up aims to explore the quintessence of Tailwind UI in today's digital age.

Understanding SOAP: Key Concepts and Practical Applications
16 Aug 2023
Understanding SOAP (Simple Object Access Protocol) can often prove daunting. This article seeks to demystify SOAP, exploring its core principles and its practical applications. By dissecting its structure and peeling back its layers, we can unravel its true potential and learn how to harness its capabilities in an efficient manner.
Influence of Google Fonts on UX and UI Design
3 Aug 2023
In the fusion of UX and UI design, nothing is trivial. Each element, including typography, plays a pivotal role in engaging user interaction. This article focuses on understanding the crucial influence of Google Fonts on UX & UI design. We delve into its impact on aesthetics, functionality, and overall user experience.
Human-Centered Design: Pivotal Player in the Arena of Modern Technology Development
17 Jul 2023
Human-Centered Design, a novel paradigm in modern technology development, is ensuring a revolution in software designs by prioritizing the user experience. As a core approach to problem-solving, it carefully blends technology with human needs to deliver highly-effective and usable solutions.
Unleashing the Power: A Comprehensive Guide to Mastering Affiliate Marketing
17 Jul 2023
This guide will embark you on a journey through the realm of Affiliate Marketing, illuminating its potency and progressive ways to harness it. Step into the universe where partnerships flourish, revenues stream, and brands expand, using dynamic marketing strategies.
Mastering the Art: Effective Strategies for Lead Nurturing in Tech Industries
17 Jul 2023
Lead nurturing in the tech industry is akin to conducting a symphony, where every note must be played in perfect harmony. It requires patience, precision, and a keen sense of timing. This article gears towards deciphering strategies that can help tech businesses skilfully navigate this intricate realm, fostering stronger customer connections and maximizing lead conversion.
Unleashing the Potential of CKEditor for Seamless Content Creation
5 Jul 2023
In this article, we will explore how to unleash the full potential of CKEditor for seamless content creation. CKEditor is a powerful and versatile text editor that offers a wide range of features and customization options. By understanding its capabilities and implementing best practices, we can optimize content creation processes and enhance the overall user experience. Let's dive in and discover the possibilities that CKEditor brings to the table!
Show all articles