Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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.
- Host: GitHub
- URL: https://github.com/jerrykingxyz/react-use-promise-func
- Owner: jerrykingxyz
- License: mit
- Created: 2020-06-30T05:12:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-27T04:45:10.000Z (over 2 years ago)
- Last Synced: 2024-04-24T21:22:00.119Z (7 months ago)
- Topics: promise, react
- Language: TypeScript
- Homepage:
- Size: 349 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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