mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
feat(useRafLoop): implement #1090
This commit is contained in:
parent
1653a6d9e4
commit
1ef1272d6d
@ -31,5 +31,5 @@ const Demo = () => {
|
||||
```ts
|
||||
const [stopLoop, isActive, startLoop] = useRafLoop(callback: CallableFunction, deps?: DependencyList);
|
||||
```
|
||||
* `callback` — function to call each RAF tick
|
||||
* `callback(time: number)` — function to call each RAF tick
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
/* eslint-disable */
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
|
||||
export type RafLoopReturns = [() => void, boolean, () => void];
|
||||
@ -7,8 +6,8 @@ export default function useRafLoop(callback: CallableFunction): RafLoopReturns {
|
||||
const raf = useRef<number | null>(null);
|
||||
const [isActive, setIsActive] = useState<boolean>(true);
|
||||
|
||||
function loopStep() {
|
||||
callback();
|
||||
function loopStep(time: number) {
|
||||
callback(time);
|
||||
raf.current = requestAnimationFrame(loopStep);
|
||||
}
|
||||
|
||||
|
||||
@ -99,4 +99,20 @@ describe('useRafLoop', () => {
|
||||
|
||||
expect(spy).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should pass timestamp as 1st argument of callback', () => {
|
||||
const spy = jest.fn();
|
||||
const hook = renderHook(() => useRafLoop(spy), { initialProps: false });
|
||||
|
||||
requestAnimationFrame.step();
|
||||
|
||||
act(() => {
|
||||
hook.result.current[0]();
|
||||
});
|
||||
|
||||
requestAnimationFrame.step();
|
||||
|
||||
expect(spy).toHaveBeenCalled();
|
||||
expect(typeof spy.mock.calls[0][0]).toBe('number');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user