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
25
votes
5 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
  • 6,243
  • 14
  • 50
  • 97
25
votes
3 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
  • 3,337
  • 11
  • 34
  • 69
24
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
  • 882
  • 1
  • 10
  • 22
24
votes
4 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
  • 243
  • 1
  • 2
  • 5
24
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,087
  • 1
  • 29
  • 48
23
votes
1 answer

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
  • 1,891
  • 1
  • 13
  • 26
22
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
  • 701
  • 6
  • 16
21
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…
bugalaws
  • 333
  • 2
  • 5
18
votes
2 answers

How to debug rxjs5?

On RxJS - Goals I read that their goal is better debuggability: Goals Provide more debuggable call stacks than preceding versions of RxJS I have just started to use redux-observable which is quite easier for me to understand comparing it to…
Amio.io
  • 17,083
  • 11
  • 66
  • 100
18
votes
1 answer

Can I use redux-saga's es6 generators as onmessage listener for websockets or eventsource?

I'm trying to get redux-saga working with the onmessage listener. I don't know why what I have isn't working. I have the following set-up. // sagas.js import { take, put } from 'redux-saga'; import {transactions} from "./actions"; function* foo…
user5325596
  • 2,050
  • 3
  • 18
  • 36
17
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
  • 5,355
  • 15
  • 65
  • 161
16
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
  • 343
  • 1
  • 4
  • 15
16
votes
1 answer

How can I get redux-saga to wait for two actions to happen at least once in any order?

Redux saga noob here. I need to create a saga that loads the initial state for the redux store from my API server. This involves using two async sagas: getCurrentUser and getGroups. I need to issue these ajax requests in parallel and wait for the…
momo
  • 905
  • 8
  • 8
15
votes
1 answer

The delay functionality from redux saga is not working

Im trying to use the delay functionality but I get an error that delay is not a function. Straight from the docs: import { race, call, put, delay } from 'redux-saga/effects' function* fetchPostsWithTimeout() { const {posts, timeout} =…
Tzvetlin Velev
  • 1,338
  • 1
  • 11
  • 24
15
votes
4 answers

Using redux-saga with setInterval - how and when to yield

Having just moved from thunks to sagas I'm trying to find the best way to call setTimeout and then from within that function call another function (in this case corewar.step()). This was my original code which works as I'd expect. runner =…
dougajmcdonald
  • 17,178
  • 7
  • 49
  • 87