piral-hooks-utils
Version:
Hooks and HOC for pilets and Piral instances.
21 lines • 880 B
JavaScript
import { useState, useEffect } from 'react';
/**
* Gives a full async lifecycle in a hook.
* @param action The lazy loader.
* @param resolve The resolve function getting the result from the promise.
* @returns The current executing state, and the trigger to start the async operation.
*/
export function useAsyncReplace(action, resolve = () => { }) {
const [executing, setExecuting] = useState(false);
const error = executing instanceof Error ? executing : undefined;
useEffect(() => {
if (executing === true) {
let active = true;
action().then((result) => active && resolve(result), (err) => active && setExecuting(err));
return () => (active = false);
}
return () => { };
}, [executing]);
return [executing === true, () => setExecuting(true), error];
}
//# sourceMappingURL=asyncReplace.js.map