a React hook for state around promise
// use yarn
yarn add use-promise-call
// use npm
npm install use-promise-call
-
usePromiseCall(asyncMethod, parameters, options?) => { data, error, loading, reload }
: usePromiseCall hooks to manage async status inside-
asyncMethod
: The method to run async -
parameters
: The initial parameters to asyncMethod, or function to get initial parameters -
options
: config-
manual
: boolean flag to manual trigger async methoddefault: false
-
initial
:initial data valuedefault: null
default :
{ interval = 100 }
-
-
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
const productId = '1'
const {data: product } = usePromiseCall(loadProduct, productId)
return product && <div>{product.name}</div>
const productId = '1'
const {data: product } = usePromiseCall(loadProduct, productId)
const {data: comments } = usePromiseCall(loadProductComments, productId)
return product && <div>{product.name}</div>
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>