File tree Expand file tree Collapse file tree 2 files changed +58
-1
lines changed Expand file tree Collapse file tree 2 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 2
2
"useTranslation" : " useTranslation" ,
3
3
"useMousePosition" : " useMousePosition" ,
4
4
"useTimer" : " useTimer" ,
5
- "useToggle" : " useToggle"
5
+ "useToggle" : " useToggle" ,
6
+ "useDebounce" : " useDebounce"
6
7
}
Original file line number Diff line number Diff line change
1
+ # useDebounce
2
+
3
+ ## Introduce
4
+
5
+ 일정 시간(delay) 동안 호출이 연속적으로 발생하면, 마지막 호출이 끝난 후에만 콜백 함수가 실행되도록 지연시키는 훅입니다.
6
+
7
+ ``` tsx
8
+ const useDebounce = <T extends unknown []>(
9
+ callback : (... args : T ) => void ,
10
+ delay : number
11
+ ): (... args : T ) => void
12
+ ```
13
+
14
+ - 입력 필드나 스크롤 이벤트와 같은 상황에서 ** 불필요한 렌더링이나 API 호출** 을 줄여 성능을 최적화할 수 있습니다.
15
+
16
+ ### Props
17
+
18
+ - ` callback ` : 사용자가 연속적인 동작을 멈춘 후 실행되는 콜백함수
19
+ - ` delay ` : 지연 시간(밀리초)
20
+
21
+ ### Returns
22
+
23
+ - ` (...args: T) => void ` : 디바운스된 콜백 함수
24
+
25
+ ## Examples
26
+
27
+ ``` tsx copy filename="TestComponent.tsx"
28
+ const TestComponent = () => {
29
+ const [searchText, setSearchText] = useState (' ' );
30
+ const [debouncedText, setDebouncedText] = useState (searchText );
31
+ const handleChange = (e : ChangeEvent <HTMLInputElement >) => {
32
+ const newValue = e .target .value ;
33
+ setSearchText (newValue );
34
+ debounceSearchText (newValue );
35
+ };
36
+
37
+ const debounceSearchText = useDebounce ((text : string ) => {
38
+ setDebouncedText (text );
39
+ }, 200 );
40
+
41
+ const filteredUsers = users .filter ((u ) =>
42
+ u .name .toLowerCase ().includes (debouncedText .toLowerCase ())
43
+ );
44
+
45
+ return (
46
+ <div >
47
+ <input type = " text" value = { searchText } onChange = { handleChange } />
48
+ <ul >
49
+ { filteredUsers .map ((u ) => (
50
+ <li key = { u .id } >{ u .name } </li >
51
+ ))}
52
+ </ul >
53
+ </div >
54
+ );
55
+ };
56
+ ```
You can’t perform that action at this time.
0 commit comments