如何在功能组件中编写Animated.Value.addListener?

时间:2020-01-05 09:01:47

标签: react-native animation

我知道在类组件中,我们以这种方式使用addListener:

voucher

我想知道如何在功能组件中转换Animated.Value.addListener吗?第二个问题:我应该将addListener放在useEffect挂钩中吗?

1 个答案:

答案 0 :(得分:1)

就我而言,我的操作如下

import React, { useRef, useEffect } from "react";

const AnimationBox = (props) => {
  const pan: any = useRef(new Animated.ValueXY(props.pan)).current;

  useEffect(() => {
    pan.addListener((value) => {
      console.log(value);
    });

    return () => {
      pan.removeAllListeners();
    };
  }, []): 
}