@alifaishol/use-async

React hook for managing async function states

Usage no npm install needed!

<script type="module">
  import alifaisholUseAsync from 'https://cdn.skypack.dev/@alifaishol/use-async';
</script>

README

React Hook for Managing Async State

npm

Usage

const options = {
  initialData: undefined,
  onError: (error: Error) => undefined,
  onSuccess: (data: Profile) => undefined,
  initialLoadingState: false,
}

const {
  exec,
  setData,
  state: {loading, data, error},
} = useAsync(Api.profile.getProfile, options);

useEffect(() => {
  exec('params for the original function (getProfile)');
}, [exec]);

const logout = useCallback((): void => {
  setData(undefined);
}, [setData]);