1.4.0 • Published 5 years ago
use-promise-call v1.4.0
use-promise-call
a React hook for state around promise
Install
// use yarn
yarn add use-promise-call
// use npm
npm install use-promise-callAPI
usePromiseCall
usePromiseCall(asyncMethod, parameters, options?) => { data, error, loading, reload }: usePromiseCall hooks to manage async status insideasyncMethod: The method to run async
parameters: The initial parameters to asyncMethod, or function to get initial parametersoptions: configmanual: boolean flag to manual trigger async methoddefault: false
initial:initial data valuedefault: null
default :
{ interval = 100 }
result
data: promise resolve valueerror: promise reject valueloading: status of async methodreload(parameters?): method of re-run async methodrun(parameters?): method of manual trigger async method
Demo
Base
const productId = '1'
const {data: product } = usePromiseCall(loadProduct, productId)
return product && <div>{product.name}</div>Parallel
const productId = '1'
const {data: product } = usePromiseCall(loadProduct, productId)
const {data: comments } = usePromiseCall(loadProductComments, productId)
return product && <div>{product.name}</div>Dependent
const productId = '1'
const {data: product, loading: productLoading} = usePromiseCall(loadProduct, productId)
const {data: store, loading: storeLoading} = usePromiseCall(loadStore, () => product.store.id)
return product && <div>{product.name}</div>