react-use-promise-func
v0.1.1
Published
a react hook for use promise function.
Downloads
3
Readme
react-use-promise-func
a react hook for use promise function.
Installation
you can install with npm install react-use-promise-func
import usePromiseFunc from 'react-use-promise-func'
// --- or ---
const usePromiseFunc = require(react-use-promise-func').default
Usage
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 (<div>
<p>{isLoading}</p>
<p>{JSON.stringify(error)}</p>
<p>{JSON.stringify(data)}</p>
</div>)
}
API
function usePromiseFunc<R> (
func: () => Promise<R>,
deps: DependencyList
): [
() => void,
boolean,
any,
R | undefined
];
params
func
: promise function you want to usedeps
: function dependency
return
array[0]
: wrapped functionarray[1]
: isLoadingarray[2]
: promise catch errorarray[3]
: promise resolve data