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
12
votes
3 answers

redux-saga: How to create multiple calls/side-effects programmatically for yield?

With redux-saga, one can execute multiple effects in parallel: import { call } from 'redux-saga/effects' // correct, effects will get executed in parallel const [users, repos] = yield [ call(fetch, '/users'), call(fetch, '/repos') ] How can i…
itinance
  • 9,334
  • 5
  • 44
  • 82
11
votes
1 answer

How to pass parameters with redux-saga

as an exercise for react-native & redux-saga, I am trying to develop a little Weather-Application. While fetching and presenting the data works just fine, I somehow struggle to pass a parameter from a TextInput to the final API call. The goal for…
dave
  • 123
  • 1
  • 4
11
votes
1 answer

Typescript: Use types on call() from redux-saga

How can I set the types of a function using call()? I have this funcion: export function apiFetch(url: string): Promise { return fetch(url).then(response => { if (!response.ok) throw new Error(response.statusText) …
Rumpelstinsk
  • 2,680
  • 2
  • 19
  • 44
11
votes
3 answers

Cancel of requests through saga

We provide a drop down option at the top..let's say it has options A B C. Whenever user changes the drop down option, a saga gets triggered which makes around 10 different webapi calls.( A map of calls which get executed in parallel) We use…
Sundeep B
  • 113
  • 1
  • 5
11
votes
2 answers

redux-saga: call vs fork and join

In redux-saga, for what reasons might you favor using call vs. fork and join? For example, when calling an HTTP API, what are the pros and cons of doing this: const result = yield call(apiWrapperFunction, arg1, arg2) versus this: const task = yield…
James Nail
  • 1,481
  • 14
  • 22
11
votes
2 answers

Use async react-select with redux-saga

I try to implement a async react-select (Select.Async). The problem is, we want to do the fetch in redux-saga. So if a user types something, the fetch-action should be triggered. Saga then fetches the record and saved them to the store. This works…
MethodenMann
  • 175
  • 2
  • 11
11
votes
2 answers

Reducing redux-thunk boilerplate

When writing redux-thunk functions, known as thunks there is allot of boilerplate that could be easily abstracted away. For example in most of our async API calls we are doing the following, without any side-effects: export const LOGIN_REQUEST =…
AndrewMcLagan
  • 11,928
  • 19
  • 75
  • 144
10
votes
2 answers

Return type of a `yield call`

I've noticed that the result of a yield call effect is typed as any when used as const data = yield call(f); while f is a () => Promise function. Am I missing something or is it a redux-saga typings limitation?
zerkms
  • 230,357
  • 57
  • 408
  • 498
10
votes
4 answers

React-redux: should the render always happen in the same tick as dispatching an action?

In my react-redux application, I have a controlled text input. Every time the component changes value, it dispatches an action and in the end, the value comes back through the redux loop and is rendered. In the example below this works well, but in…
OlliM
  • 6,505
  • 1
  • 32
  • 45
10
votes
3 answers

Promises in redux-saga

I found the same question here, but without a proper answer I am looking for. I am developing a simple application with CRUD operations. On the edit page, after the component gets mounted (componentDidMount()), the app dispatches an action to…
Ming Soon
  • 938
  • 2
  • 9
  • 30
9
votes
2 answers

How to handle form state with Formik and Redux-Saga

I recently started using Redux-Saga in a React project since it was new to me and I wanted to learn how it works. I also started using Formik since it appears to have eclipsed Redux-Form in popularity for managing forms in React applications. Now,…
peterlawless
  • 289
  • 3
  • 11
9
votes
2 answers

Error using redux saga, take(patternOrChannel): argument 8 is not valid channel or a valid pattern

I'm trying to use redux saga library to capture some actions but I'm having this error when I run the app: index.js:2178 uncaught at rootSaga at rootSaga at projectSaga at watchFetchRequest at takeEvery Error: take(patternOrChannel): …
octavioccl
  • 35,665
  • 6
  • 76
  • 95
9
votes
3 answers

How to run redux devtools with redux saga?

Trying to run reduxdevtools with redux saga: Getting this error: Error Before running a Saga, you must mount the Saga middleware on the Store using applyMiddleware This is my jscode: const store = createStore( reducer, …
bier hier
  • 13,692
  • 28
  • 72
  • 140
9
votes
1 answer

Why does a redux-saga use put method instead of dispatch?

The demonstration example in the README file uses a function called put() instead of dispatch() in the worker saga. ... // worker Saga: will be fired on USER_FETCH_REQUESTED actions function* fetchUser(action) { try { const user = yield…
Emad Omar
  • 530
  • 7
  • 17
9
votes
0 answers

React components unmount and remount when dispatching redux action in lifecycle methods

This occurs with React + Redux Saga application my team is working on We buy the React Isomorphic theme from Themeforest which bundles the Redux, Saga, and React Router V.4. We are now working on top of it. I have been using React for a while but…