InterviewHubPRO
Section Overview
Q122

Question 122 of 445

Part 5: React Redux State Management (101 - 134)

27%
React & Modern Frontend (445 Qs)•Part 5: React Redux State Management (101 - 134)

What is the mental model of redux-saga?

Saga is like a separate thread in your application, that's solely responsible for side effects. redux-saga is a redux middleware, which means this thread can be started, paused and cancelled from the main application with normal Redux actions, it has access to the full Redux application state and it can dispatch Redux actions as well.

Practical Code Example

javascript
// Redux Saga Mental Model: A separate background thread solely responsible for side-effects:
import { takeEvery, call, put } from 'redux-saga/effects';

function* fetchUserSaga(action) {
  try {
    const user = yield call(api.fetchUser, action.payload);
    yield put({ type: 'USER_SUCCESS', payload: user });
  } catch (e) {
    yield put({ type: 'USER_FAILED', message: e.message });
  }
}