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
+22-23Lines changed: 22 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -15,10 +15,9 @@ export { useAsyncIter, type IterationResult };
15
15
// TODO: The initial values should be able to be given as functions, having them called once on mount
16
16
17
17
/**
18
-
* `useAsyncIter` hooks up a single async iterable value into your component and its lifecycle.
19
-
*
20
-
* _Illustration:_
18
+
* `useAsyncIter` hooks up a single async iterable value to your component and its lifecycle.
21
19
*
20
+
* @example
22
21
* ```tsx
23
22
* import { useAsyncIter } from 'react-async-iterators';
24
23
*
@@ -34,37 +33,37 @@ export { useAsyncIter, type IterationResult };
34
33
* }
35
34
* ```
36
35
*
37
-
* Given an async iterable `input`, this hook will iterate it and rerender the host component upon
38
-
* each new value that becomes available together with any possible completion or error it may run into.
39
-
* If `input` is a plain (non async iterable) value, it will simply be used to render once and
40
-
* immediately.
36
+
* Given an async iterable `input`, this hook will iterate it value-by-value and update (re-render) the
37
+
* host component upon each yielded value, along with any possible completion or error it may run into.
38
+
* `input` may also be given a plain (non async iterable) value, in which case it will simply be used
39
+
* to render once and immediately, thus enabling components that can handle _"static"_ as well as
40
+
* _"changing"_ values and props seamlessly.
41
41
*
42
-
* The hook inits and maintains its current iteration process with its given `input` async iterable
43
-
* across re-renders as long as `input` is passed the same object reference each time (similar to
44
-
* the behavior of a `useEffect(() => {...}, [input])`), therefore care should be taken to avoid
45
-
* constantly recreating the iterable every render, e.g; by declaring it outside the component body,
46
-
* control __when__ it should be recreated with React's
47
-
* [`useMemo`](https://react.dev/reference/react/useMemo) or alternatively the library's
48
-
* {@link iterateFormatted `iterateFormatted`} util for only formatting the values.
49
-
* Whenever `useAsyncIter` detects a different `input` value, it automatically closes a previous
50
-
* `input` async iterable before proceeding to iterate any new `input` async iterable. The hook will
51
-
* also ensure closing a currently iterated `input` on component unmount.
42
+
* The hook initializes and maintains its iteration process with its given async iterable `input`
43
+
* across component updates as long as `input` keeps getting passed the same object reference every
44
+
* time (similar to the behavior of a `useEffect(() => {...}, [input])`), therefore care should be taken
45
+
* to avoid constantly recreating the iterable on every render, by e.g; declaring it outside the component
46
+
* body, control __when__ it should be recreated with React's
47
+
* [`useMemo`](https://react.dev/reference/react/useMemo) or alternatively use the library's
48
+
* {@link iterateFormatted `iterateFormatted`} util for a formatted version of an iterable which
49
+
* preserves its identity.
50
+
* Whenever `useAsyncIter` detects a different `input` value, it automatically closes the previous
51
+
* active async iterable and proceeds to start iteration with the new `input` async iterable. On
52
+
* component unmount, the hook will also ensure closing any currently iterated `input`.
52
53
*
53
54
* The object returned from `useAsyncIter` holds all the state from the most recent iteration
54
55
* of `input` (most recent value, whether is completed or still running, etc. - see
55
56
* {@link IterationResult `IterationResult`}).
56
57
* In case `input` is given a plain value, it will be delivered as-is within the returned
57
58
* result object's `value` property.
58
59
*
59
-
* @template TVal The type of values yielded by the passed iterable or of a plain value passed otherwise.
60
+
* @template TVal The type of values yielded by the passed iterable or type of plain value if otherwise passed.
60
61
* @template TInitVal The type of the initial value, defaults to `undefined`.
61
62
*
62
-
* @param input Any async iterable or plain value
63
-
* @param initialVal Any initial value for the hook to return prior to resolving the ___first
64
-
* emission___ of the ___first given___ async iterable, defaults to `undefined`.
63
+
* @param input Any async iterable or plain value.
64
+
* @param initialVal Any initial value for the hook to return prior to resolving the ___first emission___ of the ___first given___ async iterable, defaults to `undefined`.
65
65
*
66
-
* @returns An object with properties reflecting the current state of the iterated async iterable
67
-
* or plain value provided via `input` (see {@link IterationResult `IterationResult`})
66
+
* @returns An object with properties reflecting the current state of the iterated async iterable or plain value provided via `input` (see {@link IterationResult `IterationResult`}).
0 commit comments