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

Redux saga - difference between join and all effects

What's the difference between join and all in Redux Saga? function* fetchUserData(userId) { const postsTask = yield fork(fetchAlbums, userId) const albumsTask = yield fork(fetchPosts, userId) yield join([postsTask, albumsTask]) } function*…
vadimkorr
  • 116
  • 6
0
votes
1 answer

React native Dropdownalert with Navigation not working together in Redux Saga

I'm using react-native-dropdownalert and navigation. Both referenced using ref in App.js and working just fine. { …
electroid
  • 603
  • 8
  • 20
0
votes
1 answer

Cannot call `call` because: Either property `context` is missing in function flow error when using Redux-Saga?

I have a piece of code that is using the yield call api from redux saga which is calling a function and providing an input. The function it is calling a simple POST function that returns a response when hitting an api. The input for this function is…
cyanbird
  • 13
  • 3
0
votes
1 answer

CRA Boilerplate Cannot read property 'initialLoad' of undefined In Redux -Saga & Reselect & redux-toolkit

Getting This Error When i use a useSelector hook to fetch data. Im getting initial state undefined which cause the error. selector.ts import { createSelector } from '@reduxjs/toolkit'; import { RootState } from 'types'; import { initialState } from…
0
votes
1 answer

Best way to synchronize token fetching across redux sagas

So I want to be able to hare a common function for fetching a token, across all my sagas that hit a particular API. This is what I cam up with function* getValidAuthToken(params: TokenParams) { // Refresh token 5 seconds before actual…
Robert Lemiesz
  • 1,026
  • 2
  • 17
  • 29
0
votes
1 answer

TypeError: evt.target is undefined

I have tried to implement an AutoComplete Input Field. The corresponding documentation can be found here. If more code is needed, please let me know. However, the code involved is kind of long and I didn't want to put it all here. Error: Don't be…
masdot
  • 43
  • 7
0
votes
1 answer

Call a redux-saga generator function from .map in another function

I am trying to yield call getdata generator function from updateData, but the method is not getting called. function* getdata(action) { try { let response = yield call(doCall, { url: 'https://.....', method:…
Mahi
  • 3,748
  • 4
  • 35
  • 70
0
votes
1 answer

Error sending formData with axios and redux saga

I'm trying to send some multipart form data from a react-native app to the backend with the objective of update a profile photo. My saga call is... // react native code export function* uploadFile({ payload }) { const { uri } = payload.data; …
0
votes
1 answer

Redux-Saga takeEvery for multiple actions and one task

I have a watcher similar to this export default function* watch() { yield takeEvery(getProducts, fetchData) yield takeEvery(getUser, fetchData) } But in this form, it does not work, because fetchData must be run once and only if both…
Imp3ll
  • 41
  • 6
0
votes
2 answers

How to run dispatch on a saga function when a button is clicked?

I have a use case where I might be dispatching actions(saga) later in the game. I have an application where user fills out a form and later on when user clicks on submit that time the saga action will be called through which i will get response from…
Sofiya rao
  • 86
  • 1
  • 11
0
votes
1 answer

How do I wait for successful sign in with firebase using a popup dialoug and redux saga

I have a popup dialog where the user can sign in. It works however I want the dialog to wait until the sign in is successful before it closes. I am also using Redux and I tried adding await before emailSignInStart(email, password) but that doesn't…
Chris
  • 51
  • 1
  • 8
0
votes
0 answers

Component not re-rendering after Redux-Saga call

My component shows incorrect value from store. The request was successful and after request I've got fetching: false and contacts list in Redux state. But component doesn't update, console.log(fetching) shows true and UI shows "Loading". In Redux…
D M
  • 58
  • 7
0
votes
0 answers

Redux Saga calling same type multiple times simultaneously with different data

Below is the flow of my app: I have to fetch profile pics of creators of each posts in the list. I have an action type FETCH_USER_PROFILE. The action contains userId to be passed to api call inside saga function. Considering React application, the…
Yusuf
  • 321
  • 1
  • 7
  • 19
0
votes
2 answers

Select and Upload multiple images with Reactjs

I am working on an application in which I have to upload multiple images. I am using redux-saga and Nexjs. Right now it is uploading a single image but selecting multiple images. My Code: const inputFile = React.useRef(null); const onChangeFile =…
0
votes
1 answer

How to display a message on screen depending on the response from a saga handler

Contact Saga handler export function* handlePostContactUser(action) { try { yield call(axios.post, '*endpoint*', action.data); } catch (error) { throw error; } }; Front-end form handleSubmit function: let…
Thess
  • 93
  • 1
  • 8
1 2 3
99
100