1import { useMemo, useEffect, useCallback } from 'react';
2import { Animated } from 'react-native';
3
4export default (fromValue: number, toValue: number) => {
5  const intensity = useMemo(() => new Animated.Value(fromValue), []);
6
7  const _animate = useCallback(() => {
8    const baseAnimationConfig = {
9      duration: 2500,
10      isInteraction: false,
11      useNativeDriver: false,
12    };
13    const animateInConfig = {
14      ...baseAnimationConfig,
15      toValue,
16    };
17    const animateOutConfig = {
18      ...baseAnimationConfig,
19      toValue: fromValue,
20    };
21
22    Animated.timing(intensity, animateInConfig).start(() => {
23      Animated.timing(intensity, animateOutConfig).start(_animate);
24    });
25  }, [intensity]);
26
27  useEffect(() => {
28    _animate();
29  }, []);
30
31  return intensity;
32};
33