????????????Perform animations on component mount with useRequestAnimationFrame ⚛️React hooks ????

栏目: IT技术 · 发布时间: 4年前

内容简介:Takes care of running an animating function, provided as the first argument, while keeping track of its progress.When usedThe provided function receives the animation progress and a

useRequestAnimationFrame

Takes care of running an animating function, provided as the first argument, while keeping track of its progress.

When used useRequestAnimationFrame immediately starts a looping call the provided function by using window.requestAnimationFrame under the hood.

The provided function receives the animation progress and a next function to be performed to keep the animation alive.

The loop ends when the animation progress reaches the value of 100. (or any other value provided as finishAt , check the options)

Why? :bulb:

  • Easily manage a requestAnimationFrame function within a React component

Basic Usage

import { useRef } from 'react';
import { Alert } from 'beautiful-react-ui';
import { useRequestAnimationFrame } from 'beautiful-react-hooks'; 

const AnimationExample = () => {
   const ref = useRef();
   
   useRequestAnimationFrame((progress, next) => {
      ref.current.style.transform = `translateX(${progress}px)`;
      next();
   });

   return (
     <DisplayDemo>
      <div ref={ref}>
        <Alert color="primary" >Animating content</Alert>
      </div>
     </DisplayDemo>
   );
};

<AnimationExample />

Options

An object of options can be used as second argument to control the animation.

Please note: options.finishAt = -1 will cause an infinite animation

import { useRef } from 'react';
import { Alert } from 'beautiful-react-ui';
import { useRequestAnimationFrame } from 'beautiful-react-hooks'; 

const AnimationExample = () => {
   const ref = useRef();
   const options = {increment: 0.5, startAt: 0, finishAt: -1 };

   useRequestAnimationFrame((progress, next) => {
      ref.current.style.transform = `rotate(${progress}deg)`;
      next();
   }, options);

   return (
     <DisplayDemo>
      <div ref={ref}>
        <Alert color="primary" >Animating content</Alert>
      </div>
     </DisplayDemo>
   );
};

<AnimationExample />

onFinish callback

The hook returns an function to possibly set a callback when the animation finishes:

import { useRef, useState } from 'react';
import { Alert, Paragraph } from 'beautiful-react-ui';
import { useRequestAnimationFrame } from 'beautiful-react-hooks'; 

const AnimationExample = () => {
   const ref = useRef();
   const [isFinished, setIsFinished] = useState(false);
   const onFinish = useRequestAnimationFrame((progress, next) => {
      ref.current.style.transform = `translateX(${progress}px)`;
      next();
   });

   onFinish(() => setIsFinished(true)); 

   return (
     <DisplayDemo>
      <div ref={ref}>
        <Alert color="primary" >Animating content</Alert>
      </div>
     {isFinished && <Paragraph>Animation completed!</Paragraph>}
     </DisplayDemo>
   );
};

<AnimationExample />

:white_check_mark: Pro tip:

Control the speed of your animation by changing the increment value

Mastering the hook

:white_check_mark: When to use

  • When in need to perform requestAnimationFrame without re-rendering the component on each frame

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

腾讯方法

腾讯方法

潘东燕、王晓明 / 机械工业出版社 / 2014-12-11 / 39.00

这是国内第一本深度讲述腾讯产品研发与团队转型的书。本书介绍了腾讯三个不同生命周期的产品的开发过程,包括如何踏足新领域开发新产品;如何救活一个即将半路夭折的产品;如何让一个老产品持续盈利。本书呈现了互联网产品开发时会遇到普遍问题和解决方法,涉及大企业如何内部创业,并迅速组建新的项目团队;如何实现跨部门的合作;在面临新团队和紧急开发任务时如何提高团队沟通效率;在产品研发方面,如何定位产品、如何敏捷开发......一起来看看 《腾讯方法》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具