Add useAsync hook

This commit is contained in:
Misode
2022-05-08 05:29:16 +02:00
parent 4e51d41c54
commit 2772d967e0
8 changed files with 114 additions and 63 deletions

17
src/app/hooks/useAsync.ts Normal file
View File

@@ -0,0 +1,17 @@
import type { Inputs } from 'preact/hooks'
import { useEffect } from 'preact/hooks'
import type { AsyncState } from './useAsyncFn'
import { useAsyncFn } from './useAsyncFn'
export function useAsync<T>(
fn: () => Promise<T>,
inputs: Inputs = [],
): AsyncState<T> {
const [state, callback] = useAsyncFn<T, () => Promise<T>>(fn, inputs, { loading: true })
useEffect(() => {
callback()
}, [callback])
return state
}