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
0
votes
1 answer

How to clear form after successfully submitting Formik form using redux

I am currently working on a React project which uses Redux and Redux-saga. I am wondering how to make the form reset after successfully completing the async api call. using redux and redux saga am I using the resetform({}) but it immediately resets…
0
votes
1 answer

Actions must be plain objects. Use custom middleware for async actions Saga thunk I do have so far in my store

The problem is: I'm trying to use redux-saga in my react app, but i still has this error: Actions must be plain objects. Use custom middleware for async actions. Code it seems correct but no idea why gives that error. I'll be glad for all the help.…
Şehir Rehberi
  • 165
  • 1
  • 9
0
votes
2 answers

How to use yield put inside yield all

I want to make n number of api call and i am doing as floows try { .... yield all( action.uploadDocsBody.map(u => call(uploadDocs, { ..., }), ), ); yield put({ …
NIsham Mahsin
  • 243
  • 2
  • 12
0
votes
0 answers

Axios request sent twice when using React Saga

I am trying to send a GET request using axios and React Saga. The request fired twice This is my component file where I call the getBusinessHoursList action: import { getBusinessHoursList } from…
Selmi Med
  • 47
  • 2
  • 10
0
votes
2 answers

A 'yield' expression is only allowed in a generator body

I'm using redux-saga to fetch server api data. My question is that I'm trying to design following code. However yield put(get01Action(members)); which is commented out has following Syntax error. A 'yield' expression is only allowed in a generator…
tajihiro
  • 1,633
  • 1
  • 24
  • 40
0
votes
1 answer

Array from Object is empty. Javascript and React

I am writing some react frontend application. I want to pass just header from table object to my function. I am using react-redux and redux-saga In the reducer.js I am creating this object table, it has 2 arrays inside. Code starts in render() and…
0
votes
2 answers

How can I wait reducer update data from API on event onClick with redux and redux - Saga?

I'm struggle with the problem and I really need some help. This is my workflow: in onClick() event, I start a action to transfer a data object to reducer and to reduxSaga. When calling API is success, I use put() function in Redux Saga to update…
0
votes
1 answer

How to execute after yield take()

I'm trying redux-saga. I expected console.log("Now Called saga03!!"); executed, after I dispatch event by clicking button. import { fork, take } from 'redux-saga/effects'; export function* helloSaga(){ console.log('Hello Saga Root !!'); …
tajihiro
  • 1,633
  • 1
  • 24
  • 40
0
votes
2 answers

Uncaught ReferenceError with redux-saga

T'm trying to use redux-saga module. However I have following error. Uncaught ReferenceError: regeneratorRuntime is not defined at eval (saga.js:5) at Object../js/sagas/saga.js (saga.js:393) at __webpack_require__ (saga.js:20) at…
tajihiro
  • 1,633
  • 1
  • 24
  • 40
0
votes
2 answers

How to convert the thunk action to saga

I'm very new to redux-saga and am trying to get a simple demo working that makes an API call and performs pagination. It is my understanding that the redux-saga flow should work as follows. The Page-Size is used to restrict the number of data to be…
0
votes
1 answer

React + Redux : Page not re-rendering after setting redux state , or changing redux state

I'm performing an api call function - getCountryList(); using redux saga to get country list inside my componentDidMount() lifecycle method. This is for populating country list inside selectbox. Functions runs and sets the redux state successfully…
Shamseer Ahammed
  • 1,111
  • 1
  • 12
  • 18
0
votes
1 answer

Is it safe to edit same piece of state with two dispatches in a row

I'm using redux-thunk. I need to make a state empty and then fill it with data. For some reason, I have two separate actions for that, emptyName, addName. So what I was doing was just to dispatch them in a row,…
0
votes
1 answer

Why use redux-saga?

I'm recently getting into React.js and learning with a small project. While studying, I couldn't figure out clearly why I used react-saga or react-thunk. It is said to process redux state value asynchronously. But don't we already have async /…
KimBenGon
  • 165
  • 1
  • 10
0
votes
1 answer

Passing a generator as callback in javascript

In my redux-saga function, I need to yield a put in a callback. So I am passing a generator as a callback. But my callback is not executing. If instead, I use an anonymous function then the callback runs. This logs data: Tabletop.init({ key:…
Parth Tamane
  • 1,611
  • 10
  • 24
0
votes
0 answers

How to get around re-render cards with nested elements?

I have created a mobile application. It has a list of cards that contain a list of products. Initially, products contain incomplete information. We will have one reducer - ** cards **. In it, the initial state will be like this: state = {}; When we…
1 2 3
99
100