redux and redux saga code example

Example 1: install redux saga in react js

npm install redux-saga

Example 2: install redux saga in react js

import { createStore, applyMiddleware } from 'redux';
import { countReducer } from './counter/reducer';
import createSagaMiddleware from 'redux-saga';
import "regenerator-runtime/runtime";

function* exampleSaga() {
  console.log("Example saga reached");
}

const sagaMiddleware = createSagaMiddleware();

export const store = createStore(countReducer, applyMiddleware(sagaMiddleware));

sagaMiddleware.run(exampleSaga);

Example 3: redux-persist with saga

import { all, fork, take } from 'redux-saga/effects';
import { REHYDRATE } from 'redux-persist/lib/constants';
import { mySagaA, mySagaB, mySagaC } from './mySagas';

function* rootSaga() {
  console.log("Waiting for rehydration")
  yield take(REHYDRATE); // Wait for rehydrate to prevent sagas from running with empty store
  console.log("Rehydrated")
  yield all([
    fork(mySagaA),
    fork(mySagaB),
    fork(mySagaC),
  ]);
}