Skip to content

Commit

Permalink
fix(react): useBlockPromiseMultipleClick 훅 사용 시 Error 상황에서 isLoading값…
Browse files Browse the repository at this point in the history
…이 바뀌지 않는 문제 수정 완료
  • Loading branch information
Sangminnn committed Sep 25, 2024
1 parent daf45be commit 6b92bce
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/react/src/hooks/useBlockPromiseMultipleClick/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import { useRef, useState } from 'react';
import { useCallback, useRef, useState } from 'react';

export function useBlockPromiseMultipleClick() {
const [isLoading, setIsLoading] = useState(false);
const isClicked = useRef(false);

const blockMultipleClick = async (callback: () => Promise<unknown>) => {
if (isClicked.current) {
return;
}
const blockMultipleClick = useCallback(
async <T>(callback: () => Promise<T>) => {
if (isClicked.current) {
return;
}

isClicked.current = true;
setIsLoading(true);
isClicked.current = true;
setIsLoading(true);

await callback();

isClicked.current = false;
setIsLoading(false);
};
try {
const result = await callback();
return result;
} finally {
isClicked.current = false;
setIsLoading(false);
}
},
[],
);

return {
isLoading,
Expand Down

0 comments on commit 6b92bce

Please sign in to comment.