raf-hook
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

raf-hook

A React hook for requestAnimationFrame().

Installation

$ yarn add raf-hook

or

$ npm install --save raf-hook

Import

import { useRAF } from 'raf-hook';

Example usage

import React from 'react';
import { useRAF } from 'raf-hook';
 
export function MyRenderComponent({ active, fps }) {
  const reqId = useRAF(
    (ts) => {
      /*
        Runs at `fps` frames per second when `active` == true.
 
        ts is the frame timestamp from requestAnimationFrame
      */
    },
    active,
    fps
  );
 
  return (
    <div>
      The request id is <strong>{reqId}</strong>. It may be used with
      <code>window.cancelAnimationFrame</code> to cancel the request. This shouldn't
      be needed. The hook properly manages the requests based on the component's
      life cycle.
    </div>
  );
}

Package Sidebar

Install

npm i raf-hook

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

5.34 kB

Total Files

12

Last publish

Collaborators

  • abalmos