-
-
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
10 changed files
with
219 additions
and
64 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,33 @@ | ||
# `useRafState` | ||
|
||
React state hook that only updates state in the callback of [`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame). | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {useRafState, useMount} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const [state, setState] = useRafState({ | ||
width: 0, | ||
height: 0, | ||
}); | ||
|
||
useMount(() => { | ||
const onResize = () => { | ||
setState({ | ||
width: window.clientWidth, | ||
height: window.height, | ||
}); | ||
}; | ||
|
||
window.addEventListener('resize', onResize); | ||
|
||
return () => { | ||
window.removeEventListener('resize', onResize); | ||
}; | ||
}); | ||
|
||
return <pre>{JSON.stringify(state, null, 2)}</pre>; | ||
}; | ||
``` |
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,31 @@ | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import { useRafState, useMount } from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [state, setState] = useRafState({ x: 0, y: 0 }); | ||
|
||
useMount(() => { | ||
const onMouseMove = (event: MouseEvent) => { | ||
setState({ x: event.clientX, y: event.clientY }); | ||
}; | ||
const onTouchMove = (event: TouchEvent) => { | ||
setState({ x: event.changedTouches[0].clientX, y: event.changedTouches[0].clientY }); | ||
}; | ||
|
||
document.addEventListener('mousemove', onMouseMove); | ||
document.addEventListener('touchmove', onTouchMove); | ||
|
||
return () => { | ||
document.removeEventListener('mousemove', onMouseMove); | ||
document.removeEventListener('touchmove', onTouchMove); | ||
}; | ||
}); | ||
|
||
return <pre>{JSON.stringify(state, null, 2)}</pre>; | ||
}; | ||
|
||
storiesOf('State|useRafState', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useRafState.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,83 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks'; | ||
import { replaceRaf } from 'raf-stub'; | ||
import useRafState from '../useRafState'; | ||
|
||
interface RequestAnimationFrame { | ||
reset(): void; | ||
step(): void; | ||
} | ||
|
||
declare var requestAnimationFrame: RequestAnimationFrame; | ||
|
||
replaceRaf(); | ||
|
||
beforeEach(() => { | ||
requestAnimationFrame.reset(); | ||
}); | ||
|
||
afterEach(() => { | ||
requestAnimationFrame.reset(); | ||
}); | ||
|
||
describe('useRafState', () => { | ||
it('should be defined', () => { | ||
expect(useRafState).toBeDefined(); | ||
}); | ||
|
||
it('should only update state after requestAnimationFrame when providing an object', () => { | ||
const { result } = renderHook(() => useRafState(0)); | ||
|
||
act(() => { | ||
result.current[1](1); | ||
}); | ||
expect(result.current[0]).toBe(0); | ||
|
||
act(() => { | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(result.current[0]).toBe(1); | ||
|
||
act(() => { | ||
result.current[1](2); | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(result.current[0]).toBe(2); | ||
|
||
act(() => { | ||
result.current[1](prevState => prevState * 2); | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(result.current[0]).toBe(4); | ||
}); | ||
|
||
it('should only update state after requestAnimationFrame when providing a function', () => { | ||
const { result } = renderHook(() => useRafState(0)); | ||
|
||
act(() => { | ||
result.current[1](prevState => prevState + 1); | ||
}); | ||
expect(result.current[0]).toBe(0); | ||
|
||
act(() => { | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(result.current[0]).toBe(1); | ||
|
||
act(() => { | ||
result.current[1](prevState => prevState * 3); | ||
requestAnimationFrame.step(); | ||
}); | ||
expect(result.current[0]).toBe(3); | ||
}); | ||
|
||
it('should cancel update state on unmount', () => { | ||
const { unmount } = renderHook(() => useRafState(0)); | ||
const spyRafCancel = jest.spyOn(global, 'cancelAnimationFrame' as any); | ||
|
||
expect(spyRafCancel).not.toHaveBeenCalled(); | ||
|
||
unmount(); | ||
|
||
expect(spyRafCancel).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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
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 { useRef, useState, useCallback, Dispatch, SetStateAction } from 'react'; | ||
|
||
import useUnmount from './useUnmount'; | ||
|
||
const useRafState = <S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>] => { | ||
const frame = useRef(0); | ||
const [state, setState] = useState(initialState); | ||
|
||
const setRafState = useCallback((value: S | ((prevState: S) => S)) => { | ||
cancelAnimationFrame(frame.current); | ||
|
||
frame.current = requestAnimationFrame(() => { | ||
setState(value); | ||
}); | ||
}, []); | ||
|
||
useUnmount(() => { | ||
cancelAnimationFrame(frame.current); | ||
}); | ||
|
||
return [state, setRafState]; | ||
}; | ||
|
||
export default useRafState; |
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
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