-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Like `React.useState`, but state is only updated within animation frame.
- Loading branch information
Showing
7 changed files
with
168 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
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,30 @@ | ||
import * as React from 'react'; | ||
import { useMountEffect, useRafState } from '../..'; | ||
|
||
export const Example: React.FC = () => { | ||
const [state, setState] = useRafState({ x: 0, y: 0 }); | ||
|
||
useMountEffect(() => { | ||
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 ( | ||
<div> | ||
Below state will be updated on mouse/cursor move within animation frame | ||
<pre>{JSON.stringify(state, null, 2)}</pre> | ||
</div> | ||
); | ||
}; |
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,32 @@ | ||
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks'; | ||
import { Example } from './example.stories'; | ||
import { ImportPath } from '../../storybookUtil/ImportPath'; | ||
|
||
<Meta title="State/useRafState" component={Example} /> | ||
|
||
# useRafState | ||
|
||
Like `React.useState`, but state is only updated within animation frame. | ||
|
||
- Auto-cancel animation frame on component unmount. | ||
- SSR-friendly. | ||
|
||
#### Example | ||
|
||
<Canvas> | ||
<Story story={Example} inline /> | ||
</Canvas> | ||
|
||
## Reference | ||
|
||
```ts | ||
export function useRafState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>]; | ||
export function useRafState<S = undefined>(): [ | ||
S | undefined, | ||
Dispatch<SetStateAction<S | undefined>> | ||
]; | ||
``` | ||
|
||
#### Importing | ||
|
||
<ImportPath /> |
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,67 @@ | ||
import { act, renderHook } from '@testing-library/react-hooks/dom'; | ||
import { useRafState } from '../..'; | ||
|
||
describe('useRafState', () => { | ||
const raf = global.requestAnimationFrame; | ||
const caf = global.cancelAnimationFrame; | ||
|
||
beforeAll(() => { | ||
jest.useFakeTimers(); | ||
|
||
global.requestAnimationFrame = (cb) => setTimeout(cb); | ||
global.cancelAnimationFrame = (cb) => clearTimeout(cb); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllTimers(); | ||
}); | ||
|
||
afterAll(() => { | ||
jest.useRealTimers(); | ||
|
||
global.requestAnimationFrame = raf; | ||
global.cancelAnimationFrame = caf; | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(useRafState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useRafState()); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
|
||
it('should not update state unless animation frame', () => { | ||
const { result } = renderHook(() => useRafState<number>()); | ||
|
||
act(() => { | ||
result.current[1](1); | ||
result.current[1](2); | ||
result.current[1](3); | ||
}); | ||
|
||
expect(result.current[0]).toBeUndefined(); | ||
|
||
act(() => { | ||
jest.advanceTimersToNextTimer(); | ||
}); | ||
|
||
expect(result.current[0]).toBe(3); | ||
expect(result.all.length).toBe(2); | ||
}); | ||
|
||
it('should cancel animation frame on unmount', () => { | ||
const { result, unmount } = renderHook(() => useRafState<number>()); | ||
|
||
act(() => { | ||
result.current[1](1); | ||
result.current[1](2); | ||
result.current[1](3); | ||
}); | ||
|
||
unmount(); | ||
|
||
expect(result.current[0]).toBeUndefined(); | ||
}); | ||
}); |
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,13 @@ | ||
import { renderHook } from '@testing-library/react-hooks/server'; | ||
import { useRafState } from '../..'; | ||
|
||
describe('useRafState', () => { | ||
it('should be defined', () => { | ||
expect(useRafState).toBeDefined(); | ||
}); | ||
|
||
it('should render', () => { | ||
const { result } = renderHook(() => useRafState()); | ||
expect(result.error).toBeUndefined(); | ||
}); | ||
}); |
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,23 @@ | ||
import { Dispatch, SetStateAction } from 'react'; | ||
import { useRafCallback, useSafeState, useUnmountEffect } from '../..'; | ||
|
||
export function useRafState<S>(initialState: S | (() => S)): [S, Dispatch<SetStateAction<S>>]; | ||
export function useRafState<S = undefined>(): [ | ||
S | undefined, | ||
Dispatch<SetStateAction<S | undefined>> | ||
]; | ||
|
||
/** | ||
* Like `React.useState`, but state is only updated within animation frame. | ||
*/ | ||
export function useRafState<S>( | ||
initialState?: S | (() => S) | ||
): [S | undefined, Dispatch<SetStateAction<S>>] { | ||
const [state, innerSetState] = useSafeState(initialState); | ||
|
||
const [setState, cancelRaf] = useRafCallback(innerSetState); | ||
|
||
useUnmountEffect(cancelRaf); | ||
|
||
return [state, setState as Dispatch<SetStateAction<S>>]; | ||
} |