Questions tagged [redux-saga]

Redux-saga is a redux middleware library which designed for handling side effects in redux applications. It provides developers with the ability to manage asynchronous code synchronously.

Overview

middleware sits on top of redux applications, between actions and reducers more specifically. It mitigates application state transitions and side effects. As a result, flows are easier to manage, more efficient to execute, easier to test, and better at handling failures.

Use Case

Fetching data from API can be a common use case for using for handling side effects while the app is running in the background. This task is asynchronous as we can't be 100% sure if and when promised data will be available for our use. That results in poor user experience as we cannot guarantee the time it would take to display the data they need. By using a saga, we can stop function execution and wait until the data is ready, and then move forward to execute the next line of code. As [tag:redux saga], we'll usually want to display a loading indicator for signaling the status of the call to our users, resulting in better user experience at the end.

Example

function fetchJson(url) {
    return fetch(url)
    .then(request => request.text())
    .then(text => {
        return JSON.parse(text);
    })
    .catch(error => {
        console.log(`ERROR: ${error.stack}`);
    });
}

can be written as (with the help of libraries such as co.js)—

function * fetchJson(url) {
    try {
        let request = yield fetch(url);
        let text = yield request.text();
        return JSON.parse(text);
    }
    catch (error) {
        console.log(`ERROR: ${error.stack}`);
    }
};

Resources

  1. Redux-Saga Homepage
  2. Applying the Saga Pattern (Youtube video) By Caitie McCaffrey
  3. Original paper By Hector Garcia-Molina & Kenneth Salem
  4. A Saga on Sagas from MSDN site

Official Logo

Redux Saga

2491 questions
576
votes
11 answers

Pros/cons of using redux-saga with ES6 generators vs redux-thunk with ES2017 async/await

There is a lot of talk about the latest kid in redux town right now, redux-saga/redux-saga. It uses generator functions for listening to/dispatching actions. Before I wrap my head around it, I would like to know the pros/cons of using redux-saga…
hampusohlsson
  • 10,109
  • 5
  • 33
  • 50
160
votes
3 answers

How to get something from the state / store inside a redux-saga function?

How do I access the redux state inside a saga function? Short answer: import { select } from 'redux-saga/effects'; ... let data = yield select(stateSelectorFunction);
Adam Tal
  • 5,911
  • 4
  • 29
  • 49
149
votes
6 answers

Why use Redux-Observable over Redux-Saga?

I have used Redux-Saga. Code written with it is easy to reason so far, except JS generator function is messing up my head from time to time. From my understanding, Redux-Observable can achieve the similar job that handles side effects but without…
94
votes
3 answers

getState in redux-saga?

I have a store with a list of items. When my app first loads, I need to deserialize the items, as in create some in-memory objects based on the items. The items are stored in my redux store and handled by an itemsReducer. I'm trying to use…
Andy Ray
  • 30,372
  • 14
  • 101
  • 138
58
votes
4 answers

Redux Saga async/await pattern

I'm using async/await throughout my codebase. Because of this my api calls are defined by async functions async function apiFetchFoo { return await apiCall(...); } I would like to call this function from my saga code. It seems like I can not do…
mattnedrich
  • 7,577
  • 9
  • 39
  • 45
56
votes
4 answers

What are selectors in redux?

I am trying to follow this code in redux-saga export const getUser = (state, login) => state.entities.users[login] export const getRepo = (state, fullName) => state.entities.repos[fullName] Which is then used in the saga like this: import { getUser…
dagda1
  • 26,856
  • 59
  • 237
  • 450
54
votes
2 answers

redux-saga when to use fork?

what would be the difference between the two approaches below? export function* watchLoginUser() { yield takeEvery(USER_LOGIN, loginUser) } export function* watchLogoutUser() { yield takeEvery(USER_LOGOUT, logoutUser) } export function*…
Guilherme Miranda
  • 1,052
  • 2
  • 10
  • 19
53
votes
4 answers

Testing Library React vs Jest

I have a really big application with react(lot of pages, modals, tables,etc) and I'm using redux-saga for managing the state. I have a lote of stores and almost in all the components I use the useSelector method for getting the data from the store…
52
votes
3 answers

MVVM architectural pattern for a ReactJS application

I'm a semi-senior react and JavaScript developer, I've made several Universal react application. Today our CTO told me: Do you use a software architectural pattern for your application? I've no answer, He points to the Android team which use MVVM…
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
46
votes
2 answers

what is main difference between redux and redux toolkit, is saga required in redux toolkit?

My question is which should I use? Can Redux Toolkit replace Redux core?
LXT
  • 726
  • 1
  • 7
  • 18
39
votes
2 answers

What is the idiomatic way of starting rootSaga?

redux-saga project has been existing for a pretty long time now, but still there are a lot of confusing things about this library. And one of them is: how to start your rootSaga. For example, in the beginner tutorial rootSaga is started by yeilding…
slava shkodin
  • 499
  • 1
  • 4
  • 5
38
votes
4 answers

How do I check for token expiration and logout user?

The user can logout himself when he/she clicks on the logout button but if the token is expired he/she cant logout because in my application, the token is used in both server side and front end. When user clicks on the logout button, the token from…
Serenity
  • 3,884
  • 6
  • 44
  • 87
36
votes
2 answers

What's the difference between fork and spawn in redux-saga?

The docs say fork is an attached fork and spawn is a detached fork - how do they differ?
PaulB
  • 23,264
  • 14
  • 56
  • 75
33
votes
5 answers

React hooks: dispatch action from useEffect

My folder structure: |--App |--Components |--PageA.js |--PageB.js |--PageC.js |--common-effects |--useFetching.js I am refactoring my code to fetch data from API, using react hooks. I want to dispatch an action from useEffect in…
HarshvardhanSharma
  • 754
  • 2
  • 14
  • 28
33
votes
2 answers

redux saga selectors, how do I access state from a saga?

Similar questions have been asked before, but the answers have not been of any help to me. What are selectors in redux? How to get something from the state / store inside a redux-saga function? I think that I have a different setup since I cannot…
Winter
  • 2,407
  • 1
  • 19
  • 28
1
2 3
99 100