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. Applying the Saga Pattern (Youtube video) By Caitie McCaffrey
  2. Original paper By Hector Garcia-Molina & Kenneth Salem
  3. A Saga on Sagas from MSDN site

Official Logo

Redux Saga

2156 questions
534
votes
9 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
  • 9,346
  • 5
  • 30
  • 46
142
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,724
  • 4
  • 25
  • 48
141
votes
7 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…
77
votes
2 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
  • 26,451
  • 11
  • 86
  • 123
54
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
  • 21,477
  • 48
  • 188
  • 367
47
votes
3 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
  • 5,471
  • 8
  • 30
  • 40
47
votes
1 answer

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
  • 922
  • 1
  • 9
  • 16
37
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
  • 479
  • 4
  • 5
36
votes
2 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
  • 15,720
  • 11
  • 72
  • 103
32
votes
5 answers

How to dispatch Redux action from stateless component when route is loaded?

Goal: when loading a react-router route, dispatch a Redux action requesting asynchronic Saga worker to fetch data for the underlying stateless component of that route. Problem: stateless components are mere functions and don't have lifecycle…
Kitanotori
  • 1,531
  • 3
  • 14
  • 22
30
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
  • 20,984
  • 13
  • 54
  • 75
29
votes
3 answers

How to test API request failures with Redux Saga?

I am trying to test every scenarios my saga could follow, but i am not able to make happens the behaviors i want. This is pretty simple, i have a HTTP request (login), and i want to test the success and the failure cases by mocking my API…
Ludo
  • 4,473
  • 13
  • 42
  • 73
28
votes
3 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,000
  • 4
  • 20
  • 59
26
votes
2 answers

Asynchronous api calls with redux-saga

I am following redux-saga documentation on helpers, and so far it seems pretty straight forward, however I stumbled upon an issue when it comes to performing an api call (as you will see link to the docs points to such example) There is a part…
Ilja
  • 35,872
  • 66
  • 218
  • 401
25
votes
4 answers

How to "yield put" in redux-saga within a callback?

Because "yield"-statement isn't allowed within a callback, how can i use the "put" feature of redux-saga within a callback? I'd like to have the following callback: function onDownloadFileProgress(progress) { yield put({type: ACTIONS.S_PROGRESS,…
delete
  • 16,050
  • 14
  • 38
  • 63
1
2 3
99 100