Redux-Saga
2 minutes of reading
Redux-Saga is a library that allows you to more easily manage asynchronous actions in Redux-based applications. This tool is especially useful for complex projects.
Latest job offers
No job offerts found
Redux-Saga is a library for managing application state. It installs as middleware to Redux similarly to Redux-Thunk, but the syntax with which it is called is different.
Redux-Saga uses a rather old concept of generators, which appeared together with promises at the JS conference in 2013, but until now was not and still is not a popular solution. It seems quite difficult to understand at first, but it solves a lot of problems in the form of at least the so-called Callback Hell, i.e. nested multiple queries returning results.
Keywords in Redux-Saga
Redux-Saga uses keywords in its syntax, such as:
call, put, takeEvery, takeLatest, all
Each of these keywords is used in a function that is a generator, returning objects called Effects.
The simplest example would be an API query. In this query, we will retrieve a list of products for our e-commerce store, so that the component that uses the variable with the list of products will automatically reload.
// saga.js
import { call, takeLatest, all } from 'redux-saga/effects'
function* fetchProducts() {
const products = yield call(Api.fetch, '/products')
}
function* actionWatcher() {
yield takeLatest('GET_PRODUCTS', fetchProducts)
}
export default function* rootSaga() {
yield all([
actionWatcher(),
]);
}
// app.js
const sagaMiddleware = createSagaMiddleware();
const store = createStore(
reducer,
applyMiddleware(sagaMiddleware),
);
sagaMiddleware.run(rootSaga);
The operation of Redux Saga consists of the following:
1. A Redux store is created and we connect our sagaMiddleware to it.
2. The RootSaga using the keyword all accepts a list of other saga objects to which it listens.
3. ActionWatcher using takeLatest listens for changes in the Redux action named GET_PRODUCTS
4. The component using Redux calls the redux action
5. saga generator fetchProducts is called .
Using takeLatest is interesting in the sense - when the same action is called again the previous one is canceled. There are many other elements built into Saga that help manage asynchronicity such as debounce, throttle, delay.
When to use Redux-Saga
Saga seems to be a good replacement for Redux-Thunks, and it's worth trying it yourself in your own project. It comes in handy especially when you are dealing with a complicated interface and many actions. Keywords, or so-called saga effects, solve many problems related to asynchronicity. They can delay queries, determine their maximum frequency, cancel a query as well as many others.
Our offer
Web development
Find out moreMobile development
Find out moreE-commerce
Find out moreUX/UI Design
Find out moreOutsourcing
Find out moreRelated articles
How to handle a website security breach: immediate actions and recovery
30 Apr 2024
A website security breach can be a devastating event for any business, large or small. It ushers in not only immediate damages, but also long term trust issues. This article explores about prompt response to such crisis, and strategic steps to recover and fortify digital assets against future threats.
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.
Show all articles