Blog>
Snippets

Initializing Redux-Saga Middleware

Demonstrate how to integrate Redux-Saga into a Redux store setup, including the creation of the Saga middleware and applying it to the Redux store.
import { configureStore } from '@reduxjs/toolkit';
import createSagaMiddleware from 'redux-saga';
import counterReducer from '../features/counter/counterSlice';
import rootSaga from '../sagas/rootSaga';
This code imports necessary modules and the rootSaga file which combines all the sagas.
const sagaMiddleware = createSagaMiddleware();
Creating the Saga middleware instance.
const store = configureStore({
  reducer: {
    counter: counterReducer,
  },
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(sagaMiddleware),
});
Configuring the Redux store and applying the Saga middleware to it.
sagaMiddleware.run(rootSaga);
Starting the rootSaga to listen for actions dispatched to the store.