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.
npm i react-client-async
You can use the useAsync
hook to create a task.
console.log(useAsync(promiseFn, args, options));
Use the useAsyncMemo
hook to create a memoized async task.
const {
state: { result, pending, error },
load, stop
} = useAsyncMemo(
async ({ signal }) =>
fetch("/package.json", { signal })
.then((res) => res.json()),
[/* Function Dependencies */],
{ autoLoad: false },
);
You can use the Async
component to render an async component.
<Async
$fc={fc} // may be an async function component
$waiting={waiting} // waiting component
$fallback={fallback} // fallback component
{...props} // props for the function component
/>
Easy to wrap
a recursive async component and memoize it.
const Rec: FC<{ n: number }> = wrap(
async ({ [$signal]: signal, n }) =>
// break the recursion
(n <= 0) ? 0 : (
// delay and recursion
<>
{await delay(99, signal)}
{n} <Rec n={n - 1} /> {n}
</>
)
);
- ⏳
useAsyncIterable
hook - ⏳
AsyncIterable
component
async function* IterableComponent() {
yield* OtherIterableComponent();
yield await component1();
yield await component2();
yield <div>...</div>;
}
- 🌟 Star this repo if you like it! 🤩🤩🤩
- 👉 github.com/hk-shao/react-client-async
Looking forward to your feedback or contribution! 🚀🚀🚀
- Install
bun
runtime:npm install -g bun
- Install dependencies:
bun install
- Run demo:
bun dev
- Build demo:
bun build:app
- Build package:
bun build:lib
- Deploy demo to github pages:
bun build:app:deploy
- Publish this package to npm:
bun build:lib:publish