React hook for async operations.

Source code is hosted on GitHub

yarn add @corets/use-async

This library is built on top of this package:



Takes a function or an instance of Async, invokes it immediately, and keeps the state in sync with React:

import React from "react"
import { createAsync } from "@corets/async"
import { useAsync } from "@corets/use-async"

const sharedQuery = createAsync(async () => "some global data")
const Example = () => {
  const query = useAsync(async () => `Current date is ${new Date().toISOString()}`)
  const globalQuery = useAsync(sharedQuery, ["some", "dependencies"])
  const handleReload = () =>
  if (query.isRunning()) {
    return <h1>Loading ...</h1>
  if (query.isErrored()) {
    return <h1>There was an error :(</h1>
  return (
      Result: {query.getResult()} 
      <button onClick={handleReload}>Reload</button>

Check out Async documentation for more details.

Last updated