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.
Latest job offers
No job offerts found
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.
Our offer
Web development
Find out moreMobile development
Find out moreE-commerce
Find out moreUX/UI Design
Find out moreOutsourcing
Find out moreRelated articles
Responsive vs Adaptive Design: A comparative analysis on the optimal approach
25 Apr 2024
In the world of web design, there are two main methodologies popularly adopted by developers: responsive and adaptive design. Each offering unique capabilities, they cater to users' diverse needs. In this article, we perform a detailed comparative analysis between these two design structures, extensively discussing their pros, cons, and determining the optimal approach depending on certain variables.
Shedding Light on Dark Mode: Key Considerations and Advantages when Adapting Websites
9 Apr 2024
The digital world is increasingly embracing dark mode, with many popular websites now offering this viewing alternative. User-centric and energy-efficient, dark mode has won the heart of night owls and aesthetics-lovers alike. In this article, we illuminate the keys to adapting websites for dark mode, and explore its compelling advantages.
Native vs. Cross-Platform Development: Which Approach is the Best?
8 Apr 2024
In the ever-evolving realm of application development, coming up with the consummate approach is quite ascendant. Developers face several dilemmas, one of them being whether to opt for Native or Cross-Platform Development. This article will conduct a comparative analysis, exploring the pros and cons, to compute the optimal approach.
The Best CMS Platforms to Build and Manage A Blog
26 Mar 2024
In the world of blogging, content management systems (CMS) are critical. The right CMS can help amplify your voice, reach a broader audience, and ease website maintenance. In this article, we'll take you through the superior CMS platforms you need to build and manage a compelling blog.
An Overview of E-commerce Platforms: Which is the Best Fit for Your Business?
23 Mar 2024
E-commerce is a transformative sphere of business, especially in the digital age. This surges the importance of choosing the right e-commerce platform for your business. It's a critical decision that decides the success in the online market. This article aims to throw light on different e-commerce platforms, their strengths, weaknesses helping businesses to choose the most suitable one.
Intelligent Assistance: The Future of Human-Computer Interaction
21 Mar 2024
In the age of rapid digital transformation, Intelligent Assistance is charting a new course for human-computer interaction. From voice commands, predictive analytics to personalized recommendations, it's an emerging paradigm that's transforming our interaction with digital devices, making technology more intuitive and user-friendly. Through this exploration, we'll dive deep into its evolution and potential.
VSEO: How to Optimize Your Videos for Search Engines
21 Mar 2024
As content consumption braves new frontiers, Video Search Engine Optimization (VSEO) emerges as an essential realm for marketers and SEO specialists alike. Our comprehensive guide aims to illuminate the intricate process of optimizing video content for search engines, catering to both novices and seasoned experts.
Show all articles