Options
All
  • Public
  • Public/Protected
  • All
Menu

@aicacia/async_component-react

ts-async_component-react

license docs npm (scoped) pipelines

aicacia async component for react

Async Component

import { Async } from "@aicacia/async_component-react";

<Async
  promise={Promise.resolve("Hello, world!")}
  onSuccess={(value) => value} // displays promise result "Hello, world!"
  onPending={() => "Loading..."}
  onError={(error) => error.message} // on error so message
/>;

useAsync hook

import { useAsync } from "@aicacia/async_component-react";

function AsyncHook() {
  const result = useAsync(Promise.resolve("Hello, world!"));

  return (
    <div>
      {result
        .map((result) => {
          if (result.isOk()) {
            return result.unwrap();
          } else {
            return result.unwrapErr().message;
          }
        })
        .unwrapOr("Loading...")}
    </div>
  );
}

Generated using TypeDoc