βοΈβ³
React Async for Client
π¬ Demo
Β·
π GitHub
Β·
π¦ Package
π Introduction
This package helps you use async function without the need to migrate to βοΈ React 19
and server-side rendering!
- β¨ Supports utility hooks to create and render async tasks.
- β¨ Supports
AbortSignal
and automatic abort on re-render.
π Install
npm i react-client-async
β
useAsync
Hook
You can use the useAsync
hook to create a task.
console.log(useAsync(promiseFn, args, options));
β
useAsyncMemo
Hook
Use the useAsyncMemo
hook to create a memoized async task.
const {
load, stop,
state: { result, pending, error }
} = useAsyncMemo(({ signal }) =>
fetch("/package.json", { signal })
.then((res) => res.json()),
[ ],
{ autoLoad: false, defaultResult: null },
);
β
Async
Component
You can use the Async
component to render an async component.
<Async
$fc={fc}
$waiting={waiting}
$fallback={fallback}
{...props}
/>
π¬ Recursive Async Component Demo
Easy to wrap
a recursive async component and memoize it.
const Rec: FC<{ n: number }> = wrap(
async ({ [$signal]: signal, n }) =>
(n <= 0) ? 0 : (
<>
{await delay(99, signal)}
{n} <Rec n={n - 1} /> {n}
</>
)
);
β³ What is Next?
- β³
useAsyncIterable
hook
- β³
AsyncIterable
component
async function* IterableComponent() {
yield* OtherIterableComponent();
yield await component1();
yield await component2();
yield <div>...</div>;
}
Looking forward to your feedback or contribution! πππ
Development
Install
- Install
bun
runtime: npm install -g bun
- Install dependencies:
bun install
Run & Build
- Run demo:
bun dev
- Build demo:
bun build:app
- Build package:
bun build:lib
Deploy
- Deploy demo to github pages:
bun build:app:deploy
- Publish this package to npm:
bun build:lib:publish