Skip to content

Commit 7717b8f

Browse files
docs: standardize README examples
1 parent bf49c79 commit 7717b8f

1 file changed

Lines changed: 111 additions & 5 deletions

File tree

README.md

Lines changed: 111 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,118 @@
11
# use-react-saga
22

3-
> use-react-saga
3+
[![npm version](https://img.shields.io/npm/v/use-react-saga.svg)](https://www.npmjs.com/package/use-react-saga)
4+
[![npm downloads](https://img.shields.io/npm/dt/use-react-saga.svg)](https://www.npmjs.com/package/use-react-saga)
5+
[![Bun CI](https://github.com/therealparmesh/use-react-saga/actions/workflows/ci.yml/badge.svg)](https://github.com/therealparmesh/use-react-saga/actions/workflows/ci.yml)
6+
[![license](https://img.shields.io/npm/l/use-react-saga.svg)](LICENSE)
47

5-
[![npm](https://img.shields.io/npm/v/use-react-saga.svg)](https://www.npmjs.com/package/use-react-saga)
6-
[![npm](https://img.shields.io/npm/dt/use-react-saga.svg)](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).
710

8-
## Install
11+
## Installation
912

1013
```sh
11-
npm install use-react-saga
14+
npm install use-react-saga redux-saga
1215
```
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

Comments
 (0)