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: src/useAsyncIter/index.ts
+134-3Lines changed: 134 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,86 @@ export { useAsyncIter, type IterationResult };
8
8
9
9
// TODO: The initial value can be given as a function, which the internal `useState` would invoke as it's defined to do. So the typings should take into account it possibly being a function and if that's the case then to extract its return type instead of using the function type itself
10
10
11
+
/**
12
+
* `useAsyncIter` hooks up a single async iterable value into your component and its lifecycle.
13
+
*
14
+
* Given an async iterable `input`, this hook will iterate it and rerender the host component upon
15
+
* each new value that becomes available as well as any possible completion or error it may run into.
16
+
* If `input` is a plain (non async iterable) value, it will simply be used to render once and
17
+
* immediately.
18
+
*
19
+
* The hook inits and maintains its current iteration process across renderings as long as its
20
+
* `input` is passed the same object reference each time (similar to the behavior of a
21
+
* `useEffect(() => {...}, [input])`), therefore care should be taken to avoid constantly recreating
22
+
* the iterable every render, e.g; declaring it outside the component body or control __when__ it
23
+
* should be recreated with React's [`useMemo`](https://react.dev/reference/react/useMemo).
24
+
* Whenever `useAsyncIter` detects a different `input` value, it automatically closes a previous
25
+
* `input` async iterable before proceeding to iterate any new `input` async iterable. The hook will
26
+
* also ensure closing a currently iterated `input` on component unmount.
27
+
*
28
+
* The object returned from `useAsyncIter` holds all the state from the most recent iteration
29
+
* of `input` (most recent value, whether is completed or still running, etc. - see
30
+
* {@link IterationResult}).
31
+
* In case `input` is given a plain value, it will be delivered as-is within the returned
32
+
* result object's `value` property.
33
+
*
34
+
* @param input Any async iterable or plain value
35
+
* @param initialValue Any initial value for the hook to return prior to resolving the ___first
36
+
* emission___ of the ___first given___ async iterable, defaults to `undefined`.
37
+
*
38
+
* @returns An object with properties reflecting the current state of the iterated async iterable
39
+
* or plain value provided via `input` (see {@link IterationResult})
40
+
*
41
+
* @see {@link IterationResult}
42
+
*
43
+
* @example
44
+
* ```tsx
45
+
* // In its simplest:
46
+
*
47
+
* import { useAsyncIter } from 'react-async-iterators';
48
+
*
49
+
* function SelfUpdatingTodoList(props) {
50
+
* const { value: todos } = useAsyncIter(props.todosAsyncIter);
51
+
* return (
52
+
* <ul>
53
+
* {todos?.map(todo => (
54
+
* <li key={todo.id}>{todo.text}</li>
55
+
* ))}
56
+
* </ul>
57
+
* );
58
+
* }
59
+
* ```
60
+
*
61
+
* @example
62
+
* ```tsx
63
+
* // With an `initialValue` and showing usage of all properties of the returned iteration object:
64
+
*
65
+
* import { useAsyncIter } from 'react-async-iterators';
0 commit comments