Redux-Saga is a middleware library for Redux, designed to manage application side effects (like asynchronous data fetching, accessing the browser cache, or impure actions) in a more organized and testable manner. It leverages ES6 Generators to make asynchronous flows look like synchronous code, improving readability and maintainability. The current stable version is 1.4.2, with patch releases occurring relatively frequently and minor versions every few months. Its key differentiators include its declarative effect model, powerful concurrency control patterns (e.g., `takeEvery`, `takeLatest`), and robust testing utilities, providing a structured alternative to Redux Thunk for complex side effect management.
npm install redux-sagaVerified import paths — ran on the pinned version, not inferred.
This quickstart sets up a basic Redux store with `redux-saga` middleware. It demonstrates defining a simple counter reducer, creating an asynchronous saga using generator functions and effect creators (`call`, `put`, `takeEvery`), and running the saga with `sagaMiddleware.run()`.
Ensure all imports are from the official public entry points (e.g., `redux-saga` or `redux-saga/effects`). Avoid importing directly from deep paths like `redux-saga/lib/utils/someInternalFile`.
For `redux-saga@1.4.1+`, ensure `moduleResolution` is set appropriately, typically to `bundler` or `node`. If issues persist, review the `redux-saga` GitHub issues for specific `tsconfig.json` recommendations for your setup.
Consult the official `redux-saga` migration guide for detailed steps and breaking changes when upgrading from `0.x` to `1.x`.
Ensure `sagaMiddleware.run(rootSaga)` is called only once when initializing your application. If you need to restart or hot-reload sagas, consider using `sagaMiddleware.run(saga, ...args).toPromise()` to manage saga lifecycle.
Change your import statement to `import createSagaMiddleware from 'redux-saga';` instead of `import { createSagaMiddleware } from 'redux-saga';`.Ensure the saga function passed to `takeEvery` is declared as a generator function, e.g., `function* myWorkerSaga() { yield put({ type: 'ACTION' }); }`.Verify that `redux-saga` is correctly installed. Check your `tsconfig.json`'s `compilerOptions.moduleResolution` (e.g., try 'bundler' or 'node') and ensure `node_modules/@redux-saga/types` is accessible. Upgrading to `redux-saga@1.4.1` or newer might also help if you are on an older version with this issue.
Inside a saga, always `yield` effect creators from `redux-saga/effects` (e.g., `yield call(api.fetchData)` or `yield put({ type: 'ACTION' })`). Do not use `await` directly; use `yield call` for promise-based functions.