You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+75-33Lines changed: 75 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,16 @@
1
1
# @repeaterjs/react-hooks
2
-
React hooks for working with async iterators/generators, built with repeaters.
2
+
React hooks for working with async iterators/generators.
3
3
4
-
For more information about repeaters, visit [repeater.js.org](https://repeater.js.org).
4
+
These functions are implemented with repeaters. For more information about repeaters, visit [repeater.js.org](https://repeater.js.org).
5
+
6
+
## Installation
7
+
```
8
+
npm install @repeaterjs/react-hooks
9
+
```
10
+
11
+
```
12
+
yarn add @repeaterjs/react-hooks
13
+
```
5
14
6
15
## API
7
16
### `useResult`
@@ -12,23 +21,17 @@ declare function useResult<T, TDeps extends any[]>(
12
21
deps?:TDeps,
13
22
):IteratorResult<T> |undefined;
14
23
15
-
import {useResult} from"@repeaterjs/react-hooks";
24
+
import {useResult} from"@repeaterjs/react-hooks";
16
25
17
-
const result =useResult(asyncfunction*() {
26
+
const result =useResult(asyncfunction*() {
18
27
/* async generator body */
19
28
});
20
29
```
21
30
22
-
`callback` is a function which returns an async iterator, usually an async
23
-
generator function. The callback will be called as the component is constructed
24
-
and the returned iterator will update the component as each result resolves.
25
-
Returns an `IteratorResult<T>`, i.e. `{ value: T, done: boolean }`, where `T`
26
-
is the type of the emitted values, and `done` signifies whether the iterator
27
-
has returned. The first return value from this hook will be `undefined`,
28
-
signifying that the iterator has yet to emit any values.
31
+
`callback` is a function which returns an async iterator, usually an async generator function. The callback will be called as the component is constructed and the returned iterator will update the component as each result settles. `useResult` returns an `IteratorResult<T>`, an object of type `{value: T, done: boolean}`, where `T` is the type of the produced values, and `done` signifies whether the iterator . The first return value from this hook will be `undefined`, signifying that the iterator has yet to produce any values.
Similar to React’s `useEffect`, `useResult` accepts an array of dependencies as
48
-
a second argument. However, rather than being referenced via closure, the
49
-
dependencies are passed into the callback as an async iterator which updates
50
-
whenever any of the dependencies change. We pass the dependencies in manually
51
-
because `callback` is only called once, and dependencies referenced via closure
52
-
become stale as the component updates.
48
+
Similar to React’s `useEffect`, `useResult` accepts an array of dependencies as a second argument. However, rather than being referenced via closure, the dependencies are passed into the callback as an async iterator which updates whenever any of the dependencies change. We pass the dependencies in manually because `callback` is only called once, and dependencies referenced via closure become stale as the component updates.
53
49
54
-
```js
50
+
```ts
55
51
function ProductDetail({productId}) {
56
-
constresult=useResult(asyncfunction*(deps) {
52
+
const result =useResult(asyncfunction*(deps) {
57
53
forawait (const [productId] ofdeps) {
58
54
const data =awaitfetchProductData(productId);
59
55
yielddata.description;
@@ -64,13 +60,19 @@ function ProductDetail({productId}) {
The same as `useResult`, except that the value is returned rather than the `IteratorResult` object. Use `useValue` over `useResult` when you don’t care if the iterator has produced a value or returned.
74
76
75
77
### `useAsyncIter`
76
78
@@ -80,14 +82,54 @@ declare function useAsyncIter<T, TDeps extends any[]>(
Similar to `useResult`, except that `useAsyncIter` returns the async iterator
87
-
rather than consuming it. The returned async iterator can be referenced via
88
-
closure in later `useResult` calls.
92
+
Similar to `useResult`, except that `useAsyncIter` returns the async iterator rather than consuming it. The returned async iterator can be referenced via closure in further `useResult` calls.
0 commit comments