1.2.2 • Published 3 years ago

use-async-ops v1.2.2

Weekly downloads
5
License
MIT
Repository
github
Last release
3 years ago

use-async-ops

use-async-ops is a library for performing asynchronous operations in React applications utilizing hooks. This project is the successor to async-ops which was tightly coupled to Redux and Redux Sagas. With the help of hooks, this project was created to remove those dependencies and open up new possibilites along with simplify the code base. The actions dispatched by async-ops can be enabled with the use-async-ops-redux middleware.

Install

  npm install --save use-async-ops

useAsyncOp

The useAsyncOp hook provides the user with the asynchronous operation that has been previously registered as well as operation state values, including loading, error, and result.

import { useAsyncOp } from 'async-ops'

const Test = () => {
  const { call, loading, result, error } = useAsyncOp({ name: 'opName' })
  ...
}
Argument NameTypeDescription
namestringthe name of the operation
Return NameTypeDescription
callfunctionAn asynchronous method which wraps the operation
loadingbooleanboolean representing the loading state
resultanythe result returned by the operation
erroranythe error returned by the operation

useAsyncEffect

The useAsyncEffect hook is a convenience hook to perform the common pattern of using useEffect to load data when a component mounts. Instead of returning the call function, it will invoke it any time the hook arguments change and return the result and status fields just like useAsyncOp.

import { useAsyncEffect } from 'use-async-ops'

const Test = () => {
  const { loading, error, result } = useAsyncEffect({ name: 'opName' }, ['arg1', 'arg2'])
  ...
}
Argument NameTypeDescription
namestringthe name of the operation
... argsanythe rest of the arguments will be passed to the registered operation function
Return NameTypeDescription
loadingbooleanboolean representing the loading state
resultanythe result returned by the operation
erroranythe error returned by the operation
1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago