use-call
v3.0.0
Published
useCall React hook
Downloads
13
Maintainers
Readme
useCall React hook
This package is about calling functions in React components
Installation
npm install use-call
or
yarn add use-call
Usage
useCall
import { useCall } from 'use-call'
Call a function lazily with arguments and just get cached result on next renders
It takes a function and arguments: const useCall = (fn, ...args) =>
const defaultValues = useCall(getDefaultValues)
const three = useCall(sum, 1, 2) // fn, ...args
const companyPromise = useCall(fetchCompany, { companyId: 102 }) // payload
useAsyncCall
import { useAsyncCall } from 'use-call'
Call an async function and handle promise. Returns the following array: [result, error, pending]
const [company] = useAsyncCall(fetchCompany, { companyId: 234 })
const [user, userError, userLoading] = useAsyncCall(fetchUser, 120) // id
Useful packages
usePromise
hook is used to handle promises in useAsyncCall
See docs: https://github.com/doasync/use-promise
const [data, dataError, loading] = usePromise(fetchDataPromise)
Use once-only
package to create a function to be called once
See docs: https://github.com/doasync/once-only
const fetchUsersOnce = onceOnly(fetchUsers) // not in render
const usersPromise = useCall(fetchUsersOnce, 234)
Tip
If you found this hook useful, please star this package on GitHub ★
Author
@doasync