hooks 与 animejs

hooks 与 animejs

本文写于 2020 年 1 月 13 日

animejs 是现如今非常不错的一个 js 动画库。我们将其与 React Hooks 融合,使它更方便的在 React 中使用。

最终效果:

const Animate: React.FC = () => {
  const { animateTargetRef, animationRef } = useAnime({
    translateX: 300,
    loop: true,
    duration: 2000,
    autoplay: false,
  });

  useEffect(() => {
    setTimeout(() => {
      animationRef.current?.play?.();
    }, 3000);
  }, [animationRef]);

  return (
    <div
      ref={animateTargetRef}
      style={{ width: '100px', height: '100px', backgroundColor: 'black' }}
    />
  );
};

首先看看 animejs 在一般的 JS 和 HTML 中如何使用:

<div class="xxx"></div>
import anime from 'animejs';

const animation = anime({
  translateX: 300,
  loop: true,
  duration: 2000,
  autoplay: false,
  targets: '.xxx',
});

animation.play();

但是在 React 中,我们不想要到处写这些玩意儿。我们喜欢 hooks!

所以我们可以封装一个 useAnime hook。

第一步,我们可以引入 AnimeParams,让我们的 hook 拥有代码提示:

import anime, { AnimeParams } from 'animejs';

export const useAnime = (props: AnimeParams) => {
  anime(props);
};

然后我们通过 useRef 将 anime 的 targets 绑定,并且暴露出去,供其他地方使用。

//...
const animateTargetRef = useRef<any>(null);

useLayoutEffect(() => {
  if (!animationTargetRef.current) {
    console.warn('please bind animation target ref');
    return;
  }
  animate({
    ...props,
    targets: [animationTargetRef.current],
  });
}, [props]);

return { animateTargetRef };
//...

通过观察发现,animate 返回的是 AnimeInstance 类型,我们从 animejs 中导入:

import anime, { AnimeParams, AnimeInstance } from 'animejs';

// ...

const animationRef = useRef<AnimeInstance>();

// ...
animationRef.current = animate({
  ...props,
  targets: [animationTargetRef.current],
});
// ...

return { animateTargetRef, animationRef };

这样就轻松完成了 useAnime 的封装。

完整代码:

import anime, { AnimeParams, AnimeInstance } from 'animejs';
import { useRef, useLayoutEffect } from 'react';

export const useAnime = (props: AnimeParams = {}) => {
  const animateTargetRef = useRef<any>();
  const animationRef = useRef<AnimeInstance>();

  useLayoutEffect(() => {
    if (!animateTargetRef.current) {
      console.warn('please bind the anime ref while useAnime');
      return;
    }
    animationRef.current = anime({
      ...props,
      targets: [animateTargetRef.current],
    });
  }, [props]);

  return { animateTargetRef, animationRef };
};

(完)

(0)

相关推荐

  • Linux函数调用劫持的方法总结(带图)

    参考文章: https://www.cnblogs.com/LittleHann/p/3854977.html https://lwn.net/Articles/132196/ https://blo ...

  • React Ref 其实是这样的

    ref 的由来 在典型的 React 数据流中,props 是父组件与子组件交互的唯一方式.要修改一个子组件,你需要使用新的 props 来重新渲染它.但是,在某些情况下,你需要在典型数据流之外强制修 ...

  • Ref实现导航滚动定位

    摘要 在开发项目中时常有点击跳转滚动到锚点的需求,最简单的锚点定位就是给一个a标签,a标签的href = '#锚点',然后给需要跳转的锚点一个id = '锚点'.参考最简单的锚点跳转实现方式,在Rea ...

  • react hooks系列之useRef

    react hooks是 react 16.8 引入的特性,这里我们通过对react-hook-form进行分析来了解成熟的库是如何使用hook的.这将是一个系列,首先推荐 useRef 简介 在re ...

  • 如何利用 React Hooks 管理全局状态

    如何利用 React Hooks 管理全局状态 本文写于 2020 年 1 月 6 日 React 社区最火的全局状态管理库必定是 Redux,但是 Redux 本身就是为了大型管理数据而妥协设计的- ...

  • React Hooks

    Hooks介绍 之前没有用hooks写react的时候,用class类组件,state在构造函数定义,然后是钩子函数. 在这里,发现用的是函数组件,useState来定义state,useEffect ...

  • 当设计模式遇上 Hooks

    一  前言 「设计模式」是一个老生常谈的话题,但更多是集中在面向对象语言领域,如 C++,Java 等.前端领域对于设计模式的探讨热度并不是很高,很多人觉得对于 JavaScript 这种典型的面向过 ...

  • 蒲公英 · JELLY技术周刊 Vol.17: 90 行代码实现 React Hooks

    蒲公英 · JELLY技术周刊 Vol.17 React Hooks 相信大家都不陌生,自被设计出以来就备受好评,在很多场景中都有极高的使用率,其中原理更是很多大厂面试中的必考题,很多朋友都能够如数家 ...

  • 实现 React Hooks

    UI 开发有两个问题: 展示复用 逻辑复用 展示复用目前基本使用组件化来解决,逻辑复用一直以来都没有特别好的解决方案.React 从一开始的 mixin ,到 高阶组件 以及 Render Props ...

  • React实战教程之从零开始手把手教你使用 React 最新特性Hooks API 打造一款计算机知识测验App

    前言 React 框架的优雅不言而喻,组件化的编程思想使得React框架开发的项目代码简洁,易懂,但早期 React 类组件的写法略显繁琐.React Hooks 是 React 16.8 发布以来最 ...