๐ useAsync
March 12, 2020 ยท View on GitHub
Rather then litter your components with a bunch of useState calls to keep track of the state of an asynchronous function, you can use this custom hook which takes an async function as an input and returns the error, pending, and value values you need to properly update your user interface. This hook allows for both immediate execution and delayed execution using the returned execute function.
Arguments
asyncFunction: Function(): Function to make asyncimmediate?: Bool: Allows for immediate execution, default is true
Returns
error: String: Error from catch method when executing and setting valueexecute: Function(): Allows for delayed execution of your functionpending: Bool: True when executing, false otherwisevalue: Any: Value returned from execution of your function
Usage
import { useAsync } from "react-recipes";
const myFunction = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
const rnd = Math.random() * 10;
rnd <= 5
? resolve('Submitted successfully ๐')
: reject('Oh no there was an error ๐');
}, 2000);
});
};
function App() {
const {
error,
execute,
pending,
value,
} = useAsync(myFunction, false);
return (
<div>
{value && <div>{value}</div>}
{error && <div>{error}</div>}
<button onClick={execute} disabled={pending}>
{pending ? 'Loading...' : 'Click Me'}
</button>
</div>
);
}