Skip to content

Commit f3ee9bf

Browse files
committed
some JSDocs for useAsyncIterState
1 parent 2c3f74a commit f3ee9bf

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/Iterate/index.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ export { Iterate, type IterateProps };
5959
*
6060
* @param props Props for `<Iterate>`. See {@link IterateProps `IterateProps`}.
6161
*
62-
* @returns A renderable output that's re-rendered as consequent values become available and
63-
* formatted by the function passed as `children` (or otherwise the resolved values as-are).
62+
* @returns A React node that updates its contents in response to each next yielded value automatically as
63+
* formatted by the function passed as `children` (or in the absent of, just the yielded values as-are).
6464
*
6565
* @see {@link IterationResult}
6666
*

src/useAsyncIterState/index.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,52 @@
11
import { useEffect, useRef } from 'react';
22
import { IterableChannel } from './IterableChannel.js';
3+
import { type Iterate } from '../Iterate/index.js'; // eslint-disable-line @typescript-eslint/no-unused-vars
34

45
export { useAsyncIterState, type AsyncIterStateResult };
56

67
/**
7-
* ... ... ...
8+
* Basically like {@link https://react.dev/reference/react/useState `React.useState`}, only that the value
9+
* is provided back __wrapped as an async iterable__.
10+
*
11+
* This hook allows a component to declare and manage a piece of state while easily letting it control
12+
* what area(s) specifically within the UI would be bound to it (will re-render in reaction to changes in it) -
13+
* combined for example with one or more {@link Iterate `<Iterate>`}s.
14+
*
15+
* ```tsx
16+
* import { useAsyncIterState, Iterate } from 'async-react-iterators';
17+
*
18+
* function MyForm() {
19+
* const [firstNameIter, setFirstName] = useAsyncIterState<string>();
20+
* const [lastNameIter, setLastName] = useAsyncIterState<string>();
21+
* return (
22+
* <div>
23+
* <form>
24+
* <FirstNameInput valueIter={firstNameIter} onChange={setFirstName} />
25+
* <LastNameInput valueIter={lastNameIter} onChange={setLastName} />
26+
* </form>
27+
*
28+
* Greetings, <Iterate>{firstNameIter}</Iterate> <Iterate>{lastNameIter}</Iterate>
29+
* </div>
30+
* )
31+
* }
32+
* ```
33+
*
34+
* This is unlike vanila `React.useState` which simply re-renders the entire component. Instead,
35+
* `useAsyncIterState` helps confine UI updates as well as facilitate layers of sub-components that pass
36+
* actual async iterables across one another as props, skipping typical cascading re-renderings down to
37+
* __only the inner-most leafs__ of the UI tree.
38+
*
39+
* The returned async iterable is sharable; it can be iterated by multiple consumers concurrently
40+
* (e.g multiple {@link Iterate `<Iterate>`}s) all see the same yields at the same time.
41+
*
42+
* The returned async iterable is automatically closed on host component unmount.
43+
*
44+
* @template TVal the type of state to be set and yielded by returned iterable.
45+
*
46+
* @returns a stateful async iterable and a function to update it (yielding the updated value), both maintain
47+
* stable references across re-renders.
48+
*
49+
* @see {@link Iterate `<Iterate>`}
850
*/
951
function useAsyncIterState<TVal>(): AsyncIterStateResult<TVal> {
1052
const ref = useRef<{

0 commit comments

Comments
 (0)