Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native animation => new Animated.Value() with useState or not?

What is the best way to use Animated.Value () in react-native ?

With useState() or not ?

const [fadeAnim] = useState(new Animated.Value(0))

Or

const fadeAnim = new Animated.Value(0)

React-native doc: https://facebook.github.io/react-native/docs/animations

Thanks

like image 846
Romain Deguerville Avatar asked Jan 26 '23 18:01

Romain Deguerville


2 Answers

I've been using this recently:

const [fadeAnim] = useState(() => new Animated.Value(0));

and for set value the standard:

fadeAnim.setValue(0);

Also, you might want to look at useRef too, since it is what documentation recommend:

const fadeAnim = useRef(new Animated.Value(0)).current;
/* 
... 
*/
fadeAnim.setValue(0);
like image 192
Élie Avatar answered May 01 '23 23:05

Élie


Actually , Hook are a better way , so i'd say first choise

like image 42
Tilen Avatar answered May 01 '23 23:05

Tilen