-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83076fc
commit 4009683
Showing
4 changed files
with
139 additions
and
29 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,78 @@ | ||
import { useDependencyTimeout } from '@modern-kit/react'; | ||
import { useState, DependencyList } from 'react'; | ||
|
||
# useDependencyTimeout | ||
|
||
`useTimeout`를 dependency로 관리해 사용할 수 있는 커스텀 훅 입니다. | ||
|
||
`callback` 함수, `delay` 숫자, `deps` 배열, 첫 마운트 여부를 수행하는 `{ callOnMount }`를 받습니다. | ||
|
||
Timeout을 직접 핸들링 할 수 있는 `set`, `reset`, `clear` 함수를 반환합니다. | ||
|
||
<br /> | ||
|
||
## Code | ||
[🔗 실제 구현 코드 확인](https://github.com/modern-agile-team/modern-kit/blob/main/packages/react/src/hooks/useDependencyTimeout/index.ts) | ||
|
||
## Interface | ||
```ts title="typescript" | ||
interface TimeoutOptions { | ||
callback: () => void; | ||
options: number | TimeoutOptions; | ||
deps: DependencyList[]; | ||
{ callOnMount }: { callOnMount: boolean } | ||
} | ||
``` | ||
|
||
## Usage | ||
```tsx title="typescript" | ||
import { useDependencyTimeout } from '@modern-kit/react'; | ||
|
||
const Example = () => { | ||
const [number, setNumber] = useState(0); | ||
|
||
const { set, reset, clear } = useDependencyTimeout(() => { | ||
setNumber(number + 1); | ||
}, 1000); | ||
|
||
/* | ||
* useDependencyTimeout(() => { | ||
* setNumber(number + 1); | ||
* }, { delay: 1000, enabled: true }); | ||
*/ | ||
|
||
return ( | ||
<div> | ||
<p>{number}</p> | ||
<div> | ||
<button onClick={() => set()}>set</button> | ||
<button onClick={() => reset()}>reset</button> | ||
<button onClick={() => clear()}>clear</button> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Example | ||
|
||
export const Example = ({ deps }) => { | ||
const [number, setNumber] = useState(0); | ||
|
||
const { set, reset, clear } = useDependencyTimeout(() => { | ||
setNumber(number + 1); | ||
}, { delay: 1000, enabled: true }, deps); | ||
|
||
return ( | ||
<div> | ||
<p>{number}</p> | ||
<div> | ||
<button onClick={() => set()}>set</button> | ||
<button onClick={() => reset()}>reset</button> | ||
<button onClick={() => clear()}>clear</button> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,46 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { useEffect, DependencyList } from 'react'; | ||
import { useTimeout } from 'hooks/useTimeout'; | ||
import { getTimeoutOptions } from 'hooks/useTimeout/useTimeout.utils'; | ||
import type { TimeoutOptions } from 'hooks/useTimeout/useTimeout.types'; | ||
|
||
export function useDependencyTimeout( | ||
callback: () => void, | ||
delay: number, | ||
deps: DependencyList | ||
): ReturnType<typeof useTimeout>; | ||
|
||
export function useDependencyTimeout( | ||
callback: () => void, | ||
options: TimeoutOptions, | ||
deps: DependencyList | ||
): ReturnType<typeof useTimeout>; | ||
|
||
/** | ||
* @description useTimeout를 dependency 배열로 제어하는 훅입니다. | ||
* | ||
* @param callback: 실행 할 콜백 | ||
* @param delay: timeout delay | ||
* @param deps: 의존성 배열 | ||
* @param callOnMount: 최초 마운트 시에 callback 호출 할 것인지 결정 * | ||
* @param {() => void} callback - delay 후에 실행될 함수입니다. | ||
* @param {number | TimeoutOptions} options - 밀리초(ms) 단위의 지연 시간 또는 옵션 객체(delay, enabled)입니다. | ||
* @param {DependencyList} deps - 의존성 배열 | ||
* | ||
* @return {ReturnType<typeof useTimeout>} | ||
* @example | ||
* useDependencyTimeout(callback, 300, [condition]); | ||
* | ||
* @example | ||
* const { set, reset, clear } = useDependencyTimeout(() => console.log(something), 500, deps); | ||
* useDependencyTimeout(callback, { delay: 300, enabled }, [condition]); | ||
*/ | ||
export default function useDependencyTimeout( | ||
callback: () => void, | ||
delay: number, | ||
deps: unknown[], | ||
{ callOnMount }: { callOnMount: boolean } | ||
) { | ||
const isFirstMount = useRef(true); | ||
options: number | TimeoutOptions, | ||
deps: DependencyList | ||
): ReturnType<typeof useTimeout> { | ||
const { delay, enabled } = getTimeoutOptions(options); | ||
|
||
const { set, reset, clear } = useTimeout(callback, delay); | ||
|
||
useEffect(() => { | ||
if (isFirstMount.current) { | ||
if (callOnMount) { | ||
callback(); | ||
} | ||
isFirstMount.current = false; | ||
} | ||
if (delay < 0 || !enabled) return; | ||
reset(); | ||
}, [callOnMount, callback, ...deps]); | ||
}, [delay, enabled, reset, ...deps]); | ||
Check warning on line 43 in packages/react/src/hooks/useDependencyTimeout/index.ts GitHub Actions / Build-Test
|
||
|
||
return { set, reset, clear }; | ||
} |
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