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