-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): useThrottle 훅 추가 (#474)
- Loading branch information
Showing
14 changed files
with
318 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@modern-kit/react': minor | ||
--- | ||
|
||
feat(react): useThrottle 훅 추가 - @ssi02014 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { useState } from 'react'; | ||
import { useThrottle } from '@modern-kit/react' | ||
|
||
# useThrottle | ||
|
||
`debounce`를 쉽게 사용할 수 있는 커스텀 훅입니다. | ||
|
||
<br /> | ||
|
||
## Code | ||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/react/src/hooks/useThrottle/index.ts) | ||
|
||
## Interface | ||
```ts title="typescript" | ||
interface ThrottleSettings { | ||
leading?: boolean | undefined; | ||
trailing?: boolean | undefined; | ||
} | ||
|
||
type ThrottleParameters = Parameters<typeof throttle>; | ||
``` | ||
```ts title="typescript" | ||
function useThrottle<T extends (...args: any) => any>( | ||
callback: T, | ||
wait: ThrottleParameters[1], | ||
options?: ThrottleParameters[2] | ||
): ThrottleReturnType<T>; | ||
``` | ||
|
||
## Usage | ||
```tsx title="typescript" | ||
import { useState } from 'react'; | ||
import { useThrottle } from '@modern-kit/react'; | ||
|
||
const Example = () => { | ||
const [count, setCount] = useState(1); | ||
const [throttledCount, setThrottledCount] = useState(1); | ||
|
||
const countUp = () => { | ||
setCount(count + 1); | ||
}; | ||
|
||
const throttledCountUp = useThrottle(() => { | ||
setThrottledCount(throttledCount + 1); | ||
}, 1000); | ||
|
||
return ( | ||
<div> | ||
<div style={{ display: "flex" }}> | ||
<button onClick={countUp}>버튼 클릭</button> | ||
<div style={{ width: "50px" }} /> | ||
<button onClick={throttledCountUp}>debounce 버튼 클릭</button> | ||
</div> | ||
<div> | ||
<p>count: {count}</p> | ||
<p>throttledCount: {throttledCount}</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Example | ||
|
||
export const Example = () => { | ||
const [count, setCount] = useState(1); | ||
const [throttledCount, setThrottledCount] = useState(1); | ||
const countUp = () => { | ||
setCount(count + 1); | ||
}; | ||
const throttledCountUp = useThrottle(() => { | ||
setThrottledCount(throttledCount + 1); | ||
}, 1000); | ||
return ( | ||
<div> | ||
<div style={{ display: "flex" }}> | ||
<button onClick={countUp}>버튼 클릭</button> | ||
<div style={{ width: "50px" }} /> | ||
<button onClick={throttledCountUp}>debounce 버튼 클릭</button> | ||
</div> | ||
<div> | ||
<p>count: {count}</p> | ||
<p>throttledCount: {throttledCount}</p> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
<Example /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useMemo } from 'react'; | ||
import { throttle } from 'lodash-es'; | ||
import { useUnmount } from '../useUnmount'; | ||
import { usePreservedState } from '../usePreservedState'; | ||
import { usePreservedCallback } from '../usePreservedCallback'; | ||
|
||
export type ThrottleParameters = Parameters<typeof throttle>; | ||
export type ThrottleReturnType<T extends ThrottleParameters[0]> = ReturnType< | ||
typeof throttle<T> | ||
>; | ||
|
||
/** | ||
* @description 주어진 콜백 함수를 지정된 시간 동안 쓰로틀링 처리하여 특정 시간 동안 반복 호출을 방지하는 훅입니다. | ||
* | ||
* @param {ThrottleParameters[0]} callback - 쓰로틀링할 콜백 함수입니다. | ||
* @param {ThrottleParameters[1]} wait - 쓰로틀이 적용될 시간(ms)입니다. | ||
* @param {ThrottleParameters[2]} [options={}] - 쓰로틀 동작에 영향을 주는 추가 옵션입니다. `leading(default: true)`, `trailing(default: true)`, `maxWait` 옵션을 받을 수 있습니다. | ||
* | ||
* @returns {ThrottleReturnType<T>} 쓰로틀링 된 함수가 반환됩니다. | ||
* | ||
* @example | ||
* const throttledFunction = useThrottle(callback, 300); | ||
* | ||
* throttledFunction(); // 쓰로틀링 된 콜백이 실행됩니다. | ||
*/ | ||
export function useThrottle<T extends ThrottleParameters[0]>( | ||
callback: T, | ||
wait: ThrottleParameters[1], | ||
options: ThrottleParameters[2] = {} | ||
): ThrottleReturnType<T> { | ||
const callbackAction = usePreservedCallback(callback); | ||
const preservedOptions = usePreservedState(options); | ||
|
||
const throttled = useMemo(() => { | ||
return throttle(callbackAction, wait, preservedOptions); | ||
}, [callbackAction, wait, preservedOptions]); | ||
|
||
// 언마운트 시 쓰로틀 된 함수의 보류 중인 호출을 모두 버립니다. | ||
useUnmount(() => throttled.cancel()); | ||
|
||
return throttled; | ||
} |
Oops, something went wrong.