-
Notifications
You must be signed in to change notification settings - Fork 22
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
Showing
3 changed files
with
73 additions
and
1 deletion.
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
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,20 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { useInterval } from './useInterval' | ||
import { wait } from '../utils' | ||
|
||
describe('useInterval', () => { | ||
test('表现正常', async () => { | ||
let i = 0 | ||
const { result } = renderHook(() => useInterval(() => i++, 20)) | ||
expect(result.current[0]).toBe(0) | ||
await wait(20) | ||
expect(result.current[0]).toBe(1) | ||
result.current[1].stop() | ||
await wait(40) | ||
expect(result.current[0]).toBe(1) | ||
result.current[1].start() | ||
expect(result.current[0]).toBe(2) | ||
await wait(20) | ||
expect(result.current[0]).toBe(3) | ||
}) | ||
}) |
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,51 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
import { useLatest } from 'react-use' | ||
|
||
export type UseIntervalResult<TResult> = [ | ||
TResult, | ||
{ | ||
start: () => void | ||
stop: () => void | ||
}, | ||
] | ||
|
||
export function useInterval<TResult>( | ||
callback: () => TResult, | ||
delay: number, | ||
): UseIntervalResult<TResult> { | ||
const [result, setResult] = useState<TResult>(callback) | ||
const latestCallback = useLatest(callback) | ||
const latestDelay = useLatest(delay) | ||
const interval = useRef<any>() | ||
const isFirst = useRef<boolean>(true) | ||
|
||
const stop = useCallback(() => { | ||
if (interval.current) { | ||
clearInterval(interval.current) | ||
} | ||
}, []) | ||
|
||
const start = useCallback(() => { | ||
stop() | ||
if (!isFirst.current) { | ||
setResult(latestCallback.current()) | ||
} | ||
interval.current = setInterval(() => { | ||
setResult(latestCallback.current()) | ||
}, latestDelay.current) | ||
}, []) | ||
|
||
useEffect(() => { | ||
start() | ||
return stop | ||
}, [delay]) | ||
|
||
useEffect(() => { | ||
isFirst.current = false | ||
return () => { | ||
isFirst.current = true | ||
} | ||
}, []) | ||
|
||
return [result, { start, stop }] | ||
} |