I try to make a pulse animation view that loop infinite. The following view achieve that animation. But I would like to update the animation duration while it's animated. Actually It doesn't update the looping animations. Adding duration to the useEffect dependencies doesn't works properly because it's refreshed the currently animated circles.
Like this one (Source) :
Here is my current code:
const LoopPulseView = (props: {duration: number;}) => {
const nbPulse = 6;
const [pulses, setPulses] = useState([] as any);
const [duration, setDuration] = useState(props.duration);
const [delay] = useState(duration / nbPulse);
useEffect(() => {
setDuration(props.duration);
}, [props.duration]);
const scaleOut = {
0: {
opacity: 1,
scale: 1,
},
1: {
opacity: 0,
scale: 1.5,
},
};
useEffect(() => {
const newPulses = [];
for (let p = 0; p < nbPulse; p++) {
const view = (
<Animatable.View
key={p}
duration={duration}
delay={delay * p}
style={[styles.pulse, pulseStyle]}
animation={scaleOut}
iterationCount={'infinite'}
useNativeDriver={true}
/>
);
newPulses.push(view);
}
setPulses(newPulses);
}, []);
return (
<View style={containerStyle}>
<View style={pulseWrapperStyle}>
{pulses.map((pulse: any) => {
return pulse;
})}
</View>
</View>
);
};
export default LoopPulseView;
You cannot change the props of your Animatable.View because it will trigger a render of your component.
You cannot replace all your Animatable.View because it will render your component too.
The solution is to create an array of animation where you will increase gradually the number of animations and replace the old ones.
I can give you an example.