@aicacia/async_component-react

aicacia async component for react

Usage no npm install needed!

<script type="module">
  import aicaciaAsyncComponentReact from 'https://cdn.skypack.dev/@aicacia/async_component-react';
</script>

README

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