|
1 | 1 | # use-react-saga |
2 | 2 |
|
3 | | -> use-react-saga |
| 3 | +[](https://www.npmjs.com/package/use-react-saga) |
| 4 | +[](https://www.npmjs.com/package/use-react-saga) |
| 5 | +[](https://github.com/therealparmesh/use-react-saga/actions/workflows/ci.yml) |
| 6 | +[](LICENSE) |
4 | 7 |
|
5 | | -[](https://www.npmjs.com/package/use-react-saga) |
6 | | -[](https://www.npmjs.com/package/use-react-saga) |
| 8 | +Run a Redux Saga from a React hook without creating a Redux store. Based on an |
| 9 | +idea by [@Andarist](https://github.com/Andarist). |
7 | 10 |
|
8 | | -## Install |
| 11 | +## Installation |
9 | 12 |
|
10 | 13 | ```sh |
11 | | -npm install use-react-saga |
| 14 | +npm install use-react-saga redux-saga |
12 | 15 | ``` |
| 16 | + |
| 17 | +Other package managers: |
| 18 | + |
| 19 | +```sh |
| 20 | +yarn add use-react-saga redux-saga |
| 21 | +pnpm add use-react-saga redux-saga |
| 22 | +bun add use-react-saga redux-saga |
| 23 | +``` |
| 24 | + |
| 25 | +## Usage |
| 26 | + |
| 27 | +```jsx |
| 28 | +import * as React from "react"; |
| 29 | +import { call, delay, put, select, takeLatest } from "redux-saga/effects"; |
| 30 | +import { useReactSaga } from "use-react-saga"; |
| 31 | + |
| 32 | +function* incrementAsync() { |
| 33 | + const previousState = yield select(); |
| 34 | + yield call(console.log, "Starting from", previousState); |
| 35 | + |
| 36 | + yield delay(2000); |
| 37 | + yield put({ type: "INCREMENT" }); |
| 38 | + |
| 39 | + const state = yield select(); |
| 40 | + yield call(console.log, "Finished at", state); |
| 41 | +} |
| 42 | + |
| 43 | +function* saga() { |
| 44 | + yield takeLatest("INCREMENT_ASYNC", incrementAsync); |
| 45 | +} |
| 46 | + |
| 47 | +export function Example() { |
| 48 | + const [state, dispatch] = React.useReducer((state, action) => { |
| 49 | + switch (action.type) { |
| 50 | + case "INCREMENT": |
| 51 | + return state + 1; |
| 52 | + default: |
| 53 | + return state; |
| 54 | + } |
| 55 | + }, 0); |
| 56 | + |
| 57 | + const send = useReactSaga({ state, dispatch, saga }); |
| 58 | + |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <p>Clicked {state} times!</p> |
| 62 | + <button type="button" onClick={() => send({ type: "INCREMENT_ASYNC" })}> |
| 63 | + Increment async |
| 64 | + </button> |
| 65 | + </div> |
| 66 | + ); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +TypeScript declarations are included with the package. |
| 71 | + |
| 72 | +## API |
| 73 | + |
| 74 | +### `useReactSaga({ state, dispatch, saga })` |
| 75 | + |
| 76 | +Returns a stable function for sending actions to both the reducer and the saga |
| 77 | +channel. After a Saga `put`, a following `select` waits until React has processed |
| 78 | +the state update. |
| 79 | + |
| 80 | +#### `state` |
| 81 | + |
| 82 | +The current React state exposed to Redux Saga `select` effects. |
| 83 | + |
| 84 | +#### `dispatch` |
| 85 | + |
| 86 | +The React reducer dispatch function used for Saga `put` effects. |
| 87 | + |
| 88 | +#### `saga` |
| 89 | + |
| 90 | +The root saga to run while the component is mounted. The saga starts when the |
| 91 | +hook mounts and is cancelled when it unmounts. Pass a stable saga function; |
| 92 | +replacing it does not restart the running task. |
| 93 | + |
| 94 | +## Runtime requirements |
| 95 | + |
| 96 | +- React 16.8 or newer |
| 97 | +- Redux Saga 1.5 or newer |
| 98 | +- An ESM-compatible runtime or bundler |
| 99 | + |
| 100 | +React and Redux Saga are peer dependencies. |
| 101 | + |
| 102 | +## Development |
| 103 | + |
| 104 | +Run the test suite: |
| 105 | + |
| 106 | +```sh |
| 107 | +bun run test |
| 108 | +``` |
| 109 | + |
| 110 | +Inspect the package contents before publishing: |
| 111 | + |
| 112 | +```sh |
| 113 | +bun pm pack --dry-run |
| 114 | +``` |
| 115 | + |
| 116 | +## License |
| 117 | + |
| 118 | +[MIT](LICENSE) |
0 commit comments