Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jerrykingxyz/react-use-promise-func

a react hook for use promise function.
https://github.com/jerrykingxyz/react-use-promise-func

promise react

Last synced: about 1 month ago
JSON representation

a react hook for use promise function.

Awesome Lists containing this project

README

        

# react-use-promise-func

a react hook for use promise function.

## Installation

you can install with ```npm install react-use-promise-func```

``` typescript
import usePromiseFunc from 'react-use-promise-func'
// --- or ---
const usePromiseFunc = require(react-use-promise-func').default
```

## Usage

``` typescript
import { useEffect } from 'react'
import usePromiseFunc from 'react-use-promise-func'

export default function() {
const [fn, isLoading, error, data] = usePromiseFunc(() => {
// fetch api
}, [])

useEffect(fn, [])

return (


{isLoading}


{JSON.stringify(error)}


{JSON.stringify(data)}


)
}

```

## API

``` typescript
function usePromiseFunc (
func: () => Promise,
deps: DependencyList
): [
() => void,
boolean,
any,
R | undefined
];
```

### params

* ```func```: promise function you want to use
* ```deps```: function dependency

### return

* ```array[0]```: wrapped function
* ```array[1]```: isLoading
* ```array[2]```: promise catch error
* ```array[3]```: promise resolve data