Table of contents
In Redux Saga, the redux-saga/effects
module provides various methods, also known as effects, that allow you to control the flow of your sagas. Here are some commonly used methods explained with examples in simple language:
takeEvery:
The takeEvery
effect listens for every occurrence of a specified action and runs a saga in response.
Example:
import { takeEvery } from 'redux-saga/effects'; import { FETCH_DATA, fetchDataSuccess, fetchDataFailure } from './actions'; function* fetchData() { // ... Saga logic } export function* watchFetchData() { yield takeEvery(FETCH_DATA, fetchData); }
In this example, the
watchFetchData
saga listens for everyFETCH_DATA
action and triggers thefetchData
saga each time.
takeLatest:
The
takeLatest
effect cancels any previous instances of a saga and runs only the latest instance in response to a specified action.Example:
import { takeLatest } from 'redux-saga/effects'; import { FETCH_DATA, fetchDataSuccess, fetchDataFailure } from './actions'; function* fetchData() { // ... Saga logic } export function* watchFetchData() { yield takeLatest(FETCH_DATA, fetchData); }
In this example, the
watchFetchData
saga listens forFETCH_DATA
actions but only triggers the latest instance of thefetchData
saga. It cancels any previousfetchData
sagas that are still running.
put:
The
put
effect dispatches an action to the Redux store.Example:
import { put } from 'redux-saga/effects'; import { fetchDataSuccess, fetchDataFailure } from './actions'; function* fetchData() { try { // ... Saga logic yield put(fetchDataSuccess(data)); } catch (error) { yield put(fetchDataFailure(error)); } }
In this example, the
fetchData
saga uses theput
effect to dispatch either a success or failure action to the Redux store, based on the result of the saga logic.
call:
The
call
effect is used to invoke a function, which may return a promise, and waits for it to resolve.Example:
import { call } from 'redux-saga/effects'; import { getUserData } from './api'; function* fetchData() { try { const userData = yield call(getUserData); // ... Saga logic } catch (error) { // ... Error handling } }
In this example, the
fetchData
saga uses thecall
effect to invoke thegetUserData
function, which might make an asynchronous API call. It waits for thegetUserData
function to resolve before proceeding with the saga logic.
fork:
The
fork
effect is used to create a detached task, allowing parallel execution of sagas.Example:
import { fork } from 'redux-saga/effects'; import { fetchData } from './sagas'; export function* watchFetchData() { yield fork(fetchData); }
In this example, the
watchFetchData
saga uses thefork
effect to create a detached task that runs thefetchData
saga in parallel. Thefork
effect does not block the execution of the parent saga.
select:
The
select
effect is used to access the current state of the Redux store within a saga.Example:
import { select } from 'redux-saga/effects'; function* fetchData() { const userId = yield select(state => state.user.id); // ... Saga logic }
In this example, the
fetchData
saga uses theselect
effect to retrieve theid
property from the Redux store'suser
slice. Theselect
effect allows you to access specific parts of the state and use them in your saga logic.
take:
The
take
effect is used to listen for a specific action or a pattern of actions.Example:
import { take, put } from 'redux-saga/effects'; import { FETCH_DATA, fetchDataSuccess } from './actions'; function* watchDataChanges() { while (true) { const action = yield take(FETCH_DATA); // ... Saga logic yield put(fetchDataSuccess(data)); } }
In this example, the
watchDataChanges
saga uses thetake
effect to listen forFETCH_DATA
actions. Once the action is dispatched, the saga proceeds with the specified logic and dispatches afetchDataSuccess
action to the Redux store.
These are just a few examples of how effects can be used in Redux Saga. Each of these effects plays a significant role in controlling the flow of sagas, accessing the state, and listening for specific actions. By leveraging these effects, you can orchestrate complex asynchronous behavior and manage state effectively in your Redux applications.