0.1.1 • Published 3 years ago

react-use-promise-func v0.1.1

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

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 use
  • deps: function dependency

return

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

3 years ago

0.1.0

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago