-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
6 changed files
with
210 additions
and
0 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
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,35 @@ | ||
# `useRafLoop` | ||
|
||
React hook that calls given function inside the RAF loop without re-rendering parent component if not needed. Loop stops automatically on component unmount. | ||
Provides controls to stop and start loop manually. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import * as React from 'react'; | ||
import { useRafLoop } from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [ticks, setTicks] = React.useState(0); | ||
|
||
const [loopStop, isActive, loopStart] = useRafLoop(() => { | ||
setTicks(ticks + 1); | ||
}, [ticks]); | ||
|
||
return ( | ||
<div> | ||
<div>RAF triggered: {ticks} (times)</div> | ||
<br /> | ||
<button onClick={isActive ? loopStop : loopStart}>{isActive ? 'STOP' : 'START'}</button> | ||
</div> | ||
); | ||
}; | ||
``` | ||
|
||
## Reference | ||
|
||
```ts | ||
const [stopLoop, isActive, startLoop] = useRafLoop(callback: CallableFunction, deps?: DependencyList); | ||
``` | ||
* `callback` — function to call each RAF tick | ||
|
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,24 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import ShowDocs from './util/ShowDocs'; | ||
import { useRafLoop } from '..'; | ||
|
||
const Demo = () => { | ||
const [ticks, setTicks] = React.useState(0); | ||
|
||
const [loopStop, isActive, loopStart] = useRafLoop(() => { | ||
setTicks(ticks + 1); | ||
}); | ||
|
||
return ( | ||
<div> | ||
<div>RAF triggered: {ticks} (times)</div> | ||
<br /> | ||
<button onClick={isActive ? loopStop : loopStart}>{isActive ? 'STOP' : 'START'}</button> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Side effects|useRafLoop', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useRafLoop.md')} />) | ||
.add('Demo', () => <Demo />); |
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,113 @@ | ||
import { act, renderHook } from 'react-hooks-testing-library'; | ||
import useRafLoop from '../useRafLoop'; | ||
|
||
describe('useRafLoop', () => { | ||
it('should be defined', () => { | ||
expect(useRafLoop).toBeDefined(); | ||
}); | ||
|
||
it('should call a callback constantly inside the raf loop', done => { | ||
let calls = 0; | ||
const spy = () => calls++; | ||
renderHook(() => useRafLoop(spy), { initialProps: false }); | ||
|
||
expect(calls).toEqual(0); | ||
|
||
setTimeout(() => { | ||
expect(calls).toBeGreaterThanOrEqual(5); | ||
expect(calls).toBeLessThan(10); | ||
|
||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('should return stop function, start function and loop state', () => { | ||
const hook = renderHook(() => useRafLoop(() => false), { initialProps: false }); | ||
|
||
expect(typeof hook.result.current[0]).toEqual('function'); | ||
expect(typeof hook.result.current[1]).toEqual('boolean'); | ||
expect(typeof hook.result.current[2]).toEqual('function'); | ||
}); | ||
|
||
it('first element call should stop the loop', done => { | ||
let calls = 0; | ||
const spy = () => calls++; | ||
const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); | ||
|
||
// stop the loop | ||
act(() => { | ||
hook.result.current[0](); | ||
}); | ||
|
||
setTimeout(() => { | ||
expect(calls).toEqual(0); | ||
|
||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('second element should represent loop state', done => { | ||
let calls = 0; | ||
const spy = () => calls++; | ||
const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); | ||
|
||
expect(hook.result.current[1]).toBe(true); | ||
|
||
// stop the loop | ||
act(() => { | ||
hook.result.current[0](); | ||
}); | ||
|
||
expect(hook.result.current[1]).toBe(false); | ||
setTimeout(() => { | ||
expect(calls).toEqual(0); | ||
|
||
done(); | ||
}, 100); | ||
}); | ||
|
||
it('third element call should restart loop', done => { | ||
let calls = 0; | ||
const spy = () => calls++; | ||
const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); | ||
|
||
expect(hook.result.current[1]).toBe(true); | ||
|
||
// stop the loop | ||
act(() => { | ||
hook.result.current[0](); | ||
}); | ||
|
||
setTimeout(() => { | ||
expect(hook.result.current[1]).toBe(false); | ||
expect(calls).toEqual(0); | ||
|
||
// start the loop | ||
act(() => { | ||
hook.result.current[2](); | ||
}); | ||
|
||
setTimeout(() => { | ||
expect(hook.result.current[1]).toBe(true); | ||
expect(calls).toBeGreaterThanOrEqual(5); | ||
expect(calls).toBeLessThan(10); | ||
|
||
done(); | ||
}, 100); | ||
}, 100); | ||
}); | ||
|
||
it('loop should stop itself on unmount', done => { | ||
let calls = 0; | ||
const spy = () => calls++; | ||
const hook = renderHook(() => useRafLoop(spy), { initialProps: false }); | ||
|
||
hook.unmount(); | ||
|
||
setTimeout(() => { | ||
expect(calls).toEqual(0); | ||
|
||
done(); | ||
}, 100); | ||
}); | ||
}); |
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,36 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
|
||
export type RafLoopReturns = [() => void, boolean, () => void]; | ||
|
||
export default function useRafLoop(callback: CallableFunction): RafLoopReturns { | ||
const raf = useRef<number | null>(null); | ||
const [isActive, setIsActive] = useState<boolean>(true); | ||
|
||
function loopStep() { | ||
callback(); | ||
raf.current = requestAnimationFrame(loopStep); | ||
} | ||
|
||
function loopStop() { | ||
setIsActive(false); | ||
} | ||
|
||
function loopStart() { | ||
setIsActive(true); | ||
} | ||
|
||
function clearCurrentLoop() { | ||
raf.current && cancelAnimationFrame(raf.current); | ||
} | ||
|
||
useEffect(() => clearCurrentLoop, []); | ||
|
||
useEffect(() => { | ||
clearCurrentLoop(); | ||
isActive && (raf.current = requestAnimationFrame(loopStep)); | ||
|
||
return clearCurrentLoop; | ||
}, [isActive, callback]); | ||
|
||
return [loopStop, isActive, loopStart]; | ||
} |