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

Jest spyOn for redux saga 'call' gives error

So I am trying to mock the call function from redux-saga/effects using jest and it seems to be throwing an error saying call is not declared configurable. import * as mockreduxSagaEffects from 'redux-saga/effects'; test('calls all the appropriate…
krishnanspace
  • 403
  • 1
  • 7
  • 17
0
votes
1 answer

Redux saga testing using runSaga not updating the state

So I am trying to test out a redux saga using the runSaga function. This saga gives to call to an API and stores its response in the state by calling an action. After storing it, I am retrieving the stored data using a selector and performing some…
krishnanspace
  • 403
  • 1
  • 7
  • 17
0
votes
1 answer

How to re-dispatch action in Saga like redux toolkit Automated Re-fetching

I am trying to implement redux-saga action dispatch functioning like Automated Re-fetching in the redux toolkit. When opening the app, I dispatch some actions to fetch data from the server and update the redux store. When the user stays background…
Sungpah Lee
  • 1,003
  • 1
  • 13
  • 31
0
votes
2 answers

redux-thunk: how to trigger on action?

I come from a background of working with react-saga, which has the concept of "effects" which lets us trigger action handlers in response to Redux actions being dispatched: take('MY_ACTION', myActionHandler) How would I go about doing the same…
csvan
  • 8,782
  • 12
  • 48
  • 91
0
votes
1 answer

Redux TypeError: Cannot read property 'list' of undefined

I have created a simple website where you can track currencies and add them to your favourite list. I use http://api.nbp.pl/ for that. I was told to use redux for state management. the idea is that you can click on "add to your favourites" button in…
0
votes
0 answers

How to avoid redirecting user to homepage when there is an error while submitting the form using Redux Action in React?

I wanted to avoid redirecting user to homepage when there is an error while submitting the form during dispatching with redux action. Whenever we have error I am throwing one error message but not able to stop navigating towards the homepage. I am…
James
  • 371
  • 3
  • 12
0
votes
1 answer

Api calls in redux-saga crashing redux-devtools-extension

I'm trying to make some api calls using axios and redux-saga. This is nothing I haven't done before, and redux devtools usually handles this just fine. For some reason in the current application I'm working on, any actions that trigger a saga,…
0
votes
0 answers

Dispatching Redux Saga Actions right after store initialization instead of useEffect

We have a react app which Redux + Saga for State Management + Data Fetching. Currently, we initialize the state and call all the initial API's in the useEffect of the root App Component. This causes a lot of rerenders, and some additional delay. I…
Aditya
  • 116
  • 3
0
votes
0 answers

Yield call not executing or?

I'm trying to fetch some data from a endpoint. But when i console.log my response i get undefined. And when I run console.log(g.next().value) I get this in the console {@@redux-saga/IO: true, combinator: false, type: "CALL", payload: {…}}. Is the…
ComCool
  • 773
  • 4
  • 15
  • 28
0
votes
0 answers

Generator function with yields

I'm trying to do a generator function with yield inside a Redux saga. I'm trying to get a value from my endpoint ind function g1 by calling function g2. Don't know id this is the right way to do it. I only want to get data from my endpoint if…
ComCool
  • 773
  • 4
  • 15
  • 28
0
votes
2 answers

AsyncStorage not working when implement in redux

I'm trying to save Login state when user successful loged in, they will bring to HomePage instead of LoginPage using Async Storage but it did'nt work, i tried to log the value but it did'nt work , too Here is the code Async Storage ( i use…
Lê Quốc Khánh
  • 545
  • 1
  • 6
  • 20
0
votes
0 answers

Redux saga stuck in loop

I using redux-saga for testing and learning, but weird error happend, when i use saga for log out, it show loop, why this happen? Here is saga code Logut function* logOutSaga(action){ yield put({type:'LOG_OUT'}) …
Lê Quốc Khánh
  • 545
  • 1
  • 6
  • 20
0
votes
2 answers

Need help to implement User Signup in Redux Saga

This is the first time I am working with redux saga. I have a backend route called https://localhost:5000/developers/signup. I have a signup form: import { FormEvent, useState } from 'react'; import HttpService from 'services/Http'; export default…
Akash
  • 762
  • 7
  • 25
0
votes
0 answers

Using redux-saga with @reduxjs/toolkit, Trying to use Provider but it is not working. How to use Provider in this case

Here is the code sample. I can't use Provider here in react native const configureStore = () => { const sagaMiddleware = createSagaMiddleware(); const middlewares = [sagaMiddleware]; const store = configureStore({ reducer:…
0
votes
1 answer

Action get dispatched but nothing happen

I'm trying get data from action payload to use in redux-saga but it return undefined Here is some code Action export const GetUserInfo = (user, password) => { return{ type: actionList.GET_USER_INFO, data: {user, password}, …
Lê Quốc Khánh
  • 545
  • 1
  • 6
  • 20
1 2 3
99
100