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
32
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
  • 18,144
  • 15
  • 48
  • 79
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,741
  • 5
  • 16
  • 23
30
votes
4 answers

takeEvery and takeLatest. Why? When to use? Use simultaneously?

I am not clear in when to use takeEvery and when to use takeLatest ? in redux-saga. I got a basic difference by reading the official documentation. But what is the use of creating concurrent actions in takeEvery (for example, the user clicks on a…
akhil
  • 1,649
  • 3
  • 19
  • 31
30
votes
4 answers

Multiple redux-sagas

I use react-redux and redux-saga for API calls from this example. My target is to do another API calls with different urls and to use them in different pages. How to achieve that? Sagas: import { take, put,call } from 'redux-saga/effects'; import {…
IntoTheDeep
  • 4,027
  • 15
  • 39
  • 84
30
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
  • 5,060
  • 15
  • 53
  • 85
26
votes
5 answers

How to achieve callbacks in Redux-Saga?

The scenario is, I want to redirect a user or show alert based on the success, error callbacks after dispatching an action. Below is the code using redux-thunk for the…
ZaL
  • 263
  • 1
  • 3
  • 5
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
  • 44,142
  • 92
  • 275
  • 498
25
votes
9 answers

React store.getState is not a function

Here is my code: store.js import {createStore, applyMiddleware, compose} from 'redux'; import {fromJS} from 'immutable'; import {routerMiddleware} from 'react-router-redux'; import createSagaMiddleware from 'redux-saga'; import createReducer from…
ssuhat
  • 7,387
  • 18
  • 61
  • 116
25
votes
3 answers

How to handle errors in fetch() responses with Redux-Saga?

I try to handle Unauthorized error from server using redux-saga. This is my saga: function* logIn(action) { try { const user = yield call(Api.logIn, action); yield put({type: types.LOG_IN_SUCCEEDED, user}); } catch (error) { yield…
rel1x
  • 2,351
  • 4
  • 34
  • 62
24
votes
3 answers

Cancel a saga when an action is dispatched with redux-saga

I start a timer for a stopwatch React component when a START action is dispatched: import 'babel-polyfill' import { call, put } from 'redux-saga/effects' import { delay, takeEvery, takeLatest } from 'redux-saga' import { tick, START, TICK, STOP }…
vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
24
votes
2 answers

Redux Saga hot reloading

I was working on a React & Redux project. The project used to use webpack-dev-middleware and hot middleware to hot reload. After I added Redux Saga to the project, and added saga middleware to the redux store. It seems that whenever I change the…
Kevin He
  • 871
  • 7
  • 18
23
votes
1 answer

Redux Saga Watch Multiple Action

My Saga Root looks like this export default function* root() { yield takeLatest(LOAD_SEARCHRESULTS, getSearchResults); } it watches LOAD_SEARCHRESULTS action and then calls getSearchResults function. Is there any way I can watch multiple actions…
Daman
  • 473
  • 2
  • 7
  • 17
23
votes
2 answers

Can I Have Redux-Saga and Redux-Thunk Working Together?

I was working with redux-saga but I'm with a problem: the redux-auth-wrapper needs the redux-thunk to do the redirects, so I simply added the thunk in my store: import {createStore, compose, applyMiddleware} from 'redux'; import createLogger from…
bugalaws
  • 353
  • 2
  • 5
22
votes
1 answer

Order of reducer and saga

When dispatching an action is the order when it arrives to a reducer and a saga guaranteed? Can I rely on that it first enters the reducer then the saga? Reducer: function reducer(state, action) { switch (action.type) { case…
Amio.io
  • 20,677
  • 15
  • 82
  • 117
20
votes
2 answers

Problem with select in redux-saga. Error: call: argument of type {context, fn} has undefined or null `fn`

After looking through some answers to similar questions here, I just can't get my selector to work. Here's my selector.js: export const getButtonStatus = state => state.buttonStatus; (That's the entirety of the file. I don't know if I have to…
blutarch
  • 303
  • 1
  • 2
  • 6