mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
16 lines
502 B
TypeScript
16 lines
502 B
TypeScript
import * as React from 'react'
|
|
import { useStates } from './useStates'
|
|
|
|
export function useAsyncMemo<T, D extends any[] | readonly any[]>(
|
|
factory: (dependencies: D) => T | Promise<T>,
|
|
deps: D,
|
|
initialValue: T,
|
|
): T {
|
|
const firstTime = React.useRef(true)
|
|
const state = useStates<T>(() => initialValue)
|
|
React.useEffect(() => {
|
|
if (firstTime.current) firstTime.current = false
|
|
Promise.resolve(factory(deps)).then(consumed => state.set(() => consumed))
|
|
}, deps)
|
|
return state.val
|
|
}
|