We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents f00f7bd + b925359 commit cac14d6Copy full SHA for cac14d6
src/hooks/useUnmountEffect.ts
@@ -0,0 +1,30 @@
1
+import { useEffect, useRef } from 'react';
2
+
3
+/**
4
+ * 컴포넌트가 언마운트 될 때 전달받은 인자 함수를 호출하는 훅
5
+ *
6
+ * @param callback 언마운트 시에 호출될 함수
7
+ */
8
9
+type Fn = () => void;
10
11
+const useUnmountEffect = (callback: Fn) => {
12
+ const callbackRef = useRef<null | Fn>(callback);
13
14
+ useEffect(() => {
15
+ // 최신 함수를 callbackRef에 저장
16
+ callbackRef.current = callback;
17
+ }, [callback]);
18
19
20
+ return () => {
21
+ if (callbackRef.current) {
22
+ callbackRef.current();
23
+ }
24
25
+ callbackRef.current = null;
26
+ };
27
+ }, []);
28
+};
29
30
+export default useUnmountEffect;
0 commit comments