react-client-async
TypeScript icon, indicating that this package has built-in type declarations

1.3.7Β β€’Β PublicΒ β€’Β Published

βš›οΈβ³
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()),
  [ /* Function Dependencies */ ],
  { autoLoad: false, defaultResult: null },
);

βœ… Async Component

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
/>

🎬 Recursive Async Component Demo

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}
      </>
    )
);

⏳ 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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.3.71latest

Version History

VersionDownloads (Last 7 Days)Published
1.3.71
1.3.64
1.3.51
1.3.41
1.3.31
1.3.20
1.3.11
1.3.02
1.2.22
1.2.11
1.2.01
1.1.21
1.1.11
1.1.02
1.0.91
1.0.81
1.0.71
1.0.61
1.0.50
1.0.41

Package Sidebar

Install

npm i react-client-async

Weekly Downloads

24

Version

1.3.7

License

MIT

Unpacked Size

19.7 kB

Total Files

19

Last publish

Collaborators

  • shaofun