Skip to content

useAnimationFrame

Handle the requestAnimationFrame method of the window object.

Parameters

  1. a frame request callback function.
  2. and some options:
NameTypeDescription
startOnMountBooleanstart automatically when the component is mounted (default is false).

Return Values

NameTypeDescription
isStartedBooleanindicates whether the animation frame request is starting or not.
startFunctionstart requesting.
cancelFunctioncancel requesting.
toggleFunctiontoggle between start and cancel, and it takes a boolean to force it.

Example Usage

tsx
import { useAnimationFrame } from 'react-pre-hooks';

export default function Example() {
  const frame = useAnimationFrame(() => {
    document.body.appendChild(document.createElement('span'));
  });

  return (
    <main>
      <button onClick={() => frame.toggle()}>
        {frame.isStarted ? 'cancel' : 'start'}
      </button>
    </main>
  );
}
import { useAnimationFrame } from 'react-pre-hooks';

export default function Example() {
  const frame = useAnimationFrame(() => {
    document.body.appendChild(document.createElement('span'));
  });

  return (
    <main>
      <button onClick={() => frame.toggle()}>
        {frame.isStarted ? 'cancel' : 'start'}
      </button>
    </main>
  );
}

Released under the MIT License.