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
2 answers

map redux saga results

Code I followed this stackoverflow post to understand how to use the map with the yield. my code is splitted in 3 parts: Example data citiesCode = [ { bankCityId: A305 cityId: B544 }, { bankCityId: R394 cityId: D873 }, ] 1) the…
Legeo
  • 544
  • 1
  • 15
  • 36
0
votes
1 answer

Redux saga - yield take not return action

I'm using redux saga on a react-boilerplate open source project on github and I'm using redux saga to delete users. The code works perfectly on a create-react-app but doesn't work on the react-boilerplate…
SSS
  • 3
  • 1
0
votes
1 answer

How to run redux-saga middleware on the any dispatch action?

I have defined following redux saga middleware function in 'usersaga.js' import {call, put, takeEvery, takeLatest} from "redux-saga/effects"; import {REQUEST_API_DATA, receiveApiData} from '../store/actions'; import userServiceObject from…
Asis
  • 123
  • 1
  • 12
0
votes
1 answer

How to display dynamic content html in editor using react-froala-wysiwyg

I would like to know how to display dynamic script/html content in editor using react (react-froala-wysiwyg). It displays but the dynamic data not working proper shows as ${orderdate!''} import React from "react"; import…
miyavv
  • 673
  • 8
  • 18
0
votes
1 answer

Is there a way to dispatch an action on Component Mount?

I'm using Firebase for user authentication and I want to use the onAuthStateChanged() to make a user persist even after refreshing the browser. I'm also using redux-sagas to handle async operations. Index.jsx file: import React from 'react'; import…
0
votes
1 answer

React native redux saga TypeError

First of all, I apologise for the long post, I tried to shortened as much as possible. I am trying to dispatch an action through saga, but I am getting TypeError: resultFunc.apply is not a function error. The function sets a two variables in the…
oflcad
  • 355
  • 1
  • 4
  • 15
0
votes
1 answer

A way to handle async state updates on Redux store without using redux-thunk or redux-saga middleware?

I'm all in favor of NOT solving problems you don't have. So I started learning and using React without Redux. But now my app state has grown and I'm starting to face some state management issues. It's getting hard to handle it and my App.js is full…
cbdeveloper
  • 14,014
  • 11
  • 57
  • 145
0
votes
0 answers

How to Update the state in react redux-saga

I am newbie to react, redux-saga, I have a dropdown in page Display, when I select it move to respective component (eg. policy, score), In Component Pages, I have a button Add New, on clicking it will navigate to a page as mentioned in link url ,…
0
votes
0 answers

Redux actions are not called on subsequent re-mounts

I am working on a web app, and one of the components/routes has a caching problem. There is a "master" page from which users could view the detail page. When the detail page is first constructed, it loads the data correctly, but when users go back…
Igor Shmukler
  • 491
  • 3
  • 12
0
votes
0 answers

How to convert this arrow function to normal function expression to help me understand it better

How to convert this arrow function to normal function expression to help me understand it better? export const updateDetails = data => dispatch => dispatch({ type: ActionTypes.USER_UPDATE_DETAILS_REQUEST, data }); i understand that the data is…
Omtechguy
  • 2,562
  • 3
  • 28
  • 58
0
votes
1 answer

How to go about checking for expired JWT tokens in API response using redux-saga?

What is the best way to test whether an API has failed due to expiry of JWT token in React (create-react-app), Redux and Redux-Saga? Currently I'm checking for the response's status code in the API function which looks like the following, (I've…
subham
  • 33
  • 4
0
votes
2 answers

When are throw(error) and return(value) useful in javascript iterators?

In javascript, iterators are allowed to have throw(error) and return(value) methods. return(value) gives the iterator a chance to see value, and is expected to return {value: value, done: true}. throw(error) gives the iterator a chance to see and…
conartist6
  • 387
  • 2
  • 15
0
votes
1 answer

Redux Saga good example

I'm fairly new to Redux Saga, and I'm looking around for any repositories which have been written recently using React and Redux Saga to get a good idea of how an app should look/work. Most of the articles / videos etc appear to be quite old and…
JS_Dev
  • 227
  • 1
  • 11
0
votes
2 answers

AsyncStorage pass token to header post request

I constantly get 401 unauthorized server error for my post request. It is trivial post request to write item in database. I do't know how AsyncStorage work in details, but i read it is like local storage. I know it is unencrypted, asynchronous,…
Qli
  • 159
  • 1
  • 3
  • 14
0
votes
1 answer

Why does component renderer perform 3 times when dispatching an action in useEffect

I have a function component that uses hook useEffect() for dispatch action from redux. I get the action from props, it comes there from mapDispatchToProps. const AdmRegion = props => { useEffect(() => { const { fetchRegionsListRequest } =…
mr__scrpt
  • 31
  • 3
1 2 3
99
100