From fb59827ca54752b2d5977f2e95599b8b9e42cb10 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 2 Dec 2023 01:03:30 +0800 Subject: [PATCH] refactor: add request lock for zero deps for useAsync --- client/shared/hooks/useAsync.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/client/shared/hooks/useAsync.ts b/client/shared/hooks/useAsync.ts index d7cca6ac..8ea514d4 100644 --- a/client/shared/hooks/useAsync.ts +++ b/client/shared/hooks/useAsync.ts @@ -1,4 +1,4 @@ -import { DependencyList, useEffect } from 'react'; +import { DependencyList, useEffect, useRef } from 'react'; import type { FunctionReturningPromise } from '../types'; import { useAsyncFn } from './useAsyncFn'; @@ -11,8 +11,17 @@ export function useAsync( const [state, callback] = useAsyncFn(fn, deps, { loading: true, }); + const lockRef = useRef(false); useEffect(() => { + if (lockRef.current === true) { + return; + } + + if (deps.length === 0) { + lockRef.current = true; + } + callback(); }, [callback]);