Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

use-listen-on-animation-frame

artelydev3.5kMIT2.4.0TypeScript support: included

Listen to any function at extreme frequency

useanimationframe, track function on every animation frame, interval listen function, use animation frame, use animationframe, useAnimationFrame, useanimationframe

readme

useListenOnAnimationFrame

Invoke & track your functions on every animation frame
ESM · CommonJS · 1 dependency

Github Actions Build Status npm version license: MIT npm bundle size typed

Navigation

Quick start

Installation

$ npm i use-listen-on-animation-frame

Use

Library provides 2 hooks which are useAnimationFrame, useListenOnAnimationFrame. See Usage, API and Advanced usage for more.

Basic

Run function on every animation frame

import React, { useCallback, useState } from "react";
import { useAnimationFrame } from "use-listen-on-animation-frame";

const Component = () => {
  const [date, setDate] = useState(new Date());

  const syncDate = useCallback(() => {
    setDate(new Date());
  }, []);

  useAnimationFrame(syncDate);

  return <div>{date}</div>;
};

Multiple side effects

Run function once on every animation frame, and apply multiple listeners to it. Stop and start function & listeners when you want.

import React, { useState } from "react";
import { useListenOnAnimationFrame } from "use-listen-on-animation-frame";

const getCostlyRandomNumber = () => {
  const random = Math.random() * 300;

  let counter = 0;

  for (; counter < random; counter++) {
    counter++;
  }

  return counter;
};

const Component = () => {
  const [number, setNumber] = useState(0);

  const [addListener, _removeListener, stop, start] = useListenOnAnimationFrame(
    getCostlyRandomNumber,
    { autoStart: false } // optional
  );

  useEffect(() => {
    addListener((thisFrameRandom, _previousFrameRandom) => {
      setNumber(thisFrameRandom);
    });

    addListener((thisFrameRandom) => {
      // do something;
    });

    addListener((thisFrameRandom) => {
      for (let i = 0; i < thisFrameRandom; i++) {
        // do something
      }
    });
  }, [addListener]);

  useEffect(() => {
    if (somethingBadHappened) {
      stop();
    }
  }, [stop, somethingBadHappened]);

  useEffect(() => {
    if (somethingGoodHappened) {
      start();
    }
  }, [start, somethingGoodHappened]);

  return (
    <div>
      <span>{number}</span>
      <AnotherComponent
        addFrameListener={addListener}
        stopFrameListening={stop}
      />
    </div>
  );
};

changelog

2.4.0 (2022-10-26)

Features

  • types: export all types (0584d75)

2.3.1 (2022-10-26)

Bug Fixes

  • readme: simplify basic example (c03f523)

2.3.0 (2022-10-26)

Features

2.2.2 (2022-10-26)

Bug Fixes

2.2.1 (2022-10-26)

Bug Fixes

  • readme: add missing website docs link (a263da9)

2.2.0 (2022-10-25)

Features

  • website: add pkg website (2e12a49)

2.1.2 (2022-10-25)

Bug Fixes

  • docs: move hook comparison out from details (8b92544)

2.1.1 (2022-10-25)

Bug Fixes

2.1.0 (2022-10-25)

Features

2.0.2 (2022-10-24)

Bug Fixes

  • readme: code to collapsibles (57d0a06)

2.0.1 (2022-10-24)

Bug Fixes

  • use-animation-frame: make options optional (8daee80)

2.0.0 (2022-10-24)

Features

  • hook: autoclean, new hook, fix types (3e6a75e)

BREAKING CHANGES

  • hook: no need to remove listener because of autoclean, fix types

1.5.0 (2022-10-24)

Features

  • autoclean!: simple hook, autoclean & bette readmy (31fc761)

1.4.2 (2022-10-24)

Bug Fixes

  • readme: specify timeupdate comparison (87d80be)

1.4.1 (2022-10-24)

Bug Fixes

1.4.0 (2022-10-23)

Features

1.3.4 (2022-10-23)

Bug Fixes

1.3.3 (2022-10-23)

Bug Fixes

  • readme: move previous animation frame section to advanced (18ce8a5)

1.3.2 (2022-10-23)

Bug Fixes

1.3.1 (2022-10-23)

Bug Fixes

1.3.0 (2022-10-23)

Features

  • hook: start, stop, autostart (78e0431)

1.2.0 (2022-10-23)

Features

  • should-invoke: provide previous value (40469a5)

1.1.1 (2022-10-23)

Bug Fixes

  • id: provide unique uuid generation (cbd34ee)

1.1.0 (2022-10-22)

Features

  • esm: provide esm module (137bd2b)

1.0.0 (2022-10-22)

Bug Fixes