diff --git a/docs/useRafLoop.md b/docs/useRafLoop.md index f124385b..978c5587 100644 --- a/docs/useRafLoop.md +++ b/docs/useRafLoop.md @@ -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 diff --git a/src/useRafLoop.ts b/src/useRafLoop.ts index 0eafd9a9..9760ea6b 100644 --- a/src/useRafLoop.ts +++ b/src/useRafLoop.ts @@ -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(null); const [isActive, setIsActive] = useState(true); - function loopStep() { - callback(); + function loopStep(time: number) { + callback(time); raf.current = requestAnimationFrame(loopStep); } diff --git a/tests/useRafLoop.test.tsx b/tests/useRafLoop.test.tsx index 844c7879..36f7d446 100644 --- a/tests/useRafLoop.test.tsx +++ b/tests/useRafLoop.test.tsx @@ -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'); + }); });