2.3.1 • Published 4 years ago

@nutgaard/use-async v2.3.1

Weekly downloads
37
License
MIT
Repository
github
Last release
4 years ago

use-async

styled with prettier Travis codecov dependencies Status

Installation

npm install @nutgaard/use-async --save

Usage

The library exposes one hook useAsync, and three utility-functions to help use the result (isPending, hasData and hasError).

import React from 'react';
import useAsync, { isPending, hasError } from '@nutgaard/use-async';

const source = React.useCallback((isRerun) => Promise.resolve("your data here"), []);
const result = useAsync(source);

if (isPending(result)) {
  return <Spinner />;
} else if (hasError(result)) {
  return <Error />
} 

return <pre>{result.data}</pre>

useAsync API

ArgumentTypeOptionalDefault 
source(isRerun) => Promise<DATA>No-
lazybooleanYesfalse
dependencyListArray<any>Yesundefiend

In cases where dependencyList is defined it is passed on to useEffect instead of source.
This allows a greater control of when the effect should run in cases where the source does not necessarily change.

Types

Full documentation of types can be seen here, or in the 80-ish lines of code.

Credits

Made using the awesome typescript library starter

2.3.1

4 years ago

2.3.0

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.3

5 years ago

2.1.2

5 years ago

2.1.1

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.0

5 years ago