Skip to content

Commit 78bdec8

Browse files
committed
📝 docs: useDeepCompareEffect 문서 작성
1 parent 209d8af commit 78bdec8

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

docs/pages/docs/hooks/_meta.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
2-
"useTranslation": "useTranslation",
2+
"useDeepCompareEffect": "useDeepCompareEffect",
33
"useMousePosition": "useMousePosition",
4-
"useTimer": "useTimer"
4+
"useTimer": "useTimer",
5+
"useTranslation": "useTranslation"
56
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# useDeepCompareEffect
2+
3+
## Introduce
4+
5+
의존성 배열의 깊은 비교를 통해 변화를 감지하고 콜백을 실행시키는 훅입니다.
6+
7+
```ts
8+
const useDeepCompareEffect = (callback: () => void, dependencies: unknown[]) => void
9+
```
10+
11+
### Props
12+
13+
- `callback` : 의존성 배열의 변화가 감지되었을 때 실행할 콜백 함수
14+
- `dependencies` : 의존성 배열
15+
16+
## Examples
17+
18+
```tsx copy filename="TestComponent.tsx"
19+
function App() {
20+
const [, setCount] = useState(0);
21+
const effectCountRef = useRef(0);
22+
const deepCompareCountRef = useRef(0);
23+
24+
useEffect(() => {
25+
effectCountRef.current += 1;
26+
// 빈 객체는 매번 새로운 참조를 갖기 때문에 effectCount는 계속 증가
27+
}, [{}]);
28+
29+
useDeepCompareEffect(() => {
30+
deepCompareCountRef.current += 1;
31+
// 빈 객체를 같은 값으로 판단해 deepCompareCountRef는 한 번(첫 시도)만 증가
32+
}, [{}]);
33+
34+
return (
35+
<div>
36+
<p>effectCount: {effectCountRef.current}</p>
37+
<p>deepCompareCount: {deepCompareCountRef.current}</p>
38+
<p>
39+
<button type="button" onClick={() => setCount((c) => c + 1)}>
40+
리렌더링 발생
41+
</button>
42+
</p>
43+
</div>
44+
);
45+
}
46+
```

0 commit comments

Comments
 (0)