Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Animated.Value.addListener in functional components?

I know in class components we use addListener in this way:

const Animated.Value= new Animated.Value(0);
Animated.Value.addListener((value)=>this.value=value;)

I wondering how should i convert Animated.Value.addListener in functional components?and second question: should i put addListener in useEffect hook?

like image 296
roz333 Avatar asked Oct 20 '25 21:10

roz333


1 Answers

In my case, I did as below

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();
    };
  }, []): 
}
like image 80
Saad Qamar Avatar answered Oct 24 '25 12:10

Saad Qamar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!