Building highly performant animations in React Native can be tricky, particularly when dealing with gestures. Gesture animations are tricky due to the frequency the events from the gesture handler are fired, sometimes only 30–40 milliseconds apart or less.
Let’s jump into how we can reduce costly re-renders for gesture animations.
Typically, when we want to track a value in a React component, in this case, offsetY in the example below, the default strategy is to use setState when the value changes. This isn’t optimal since a small interaction will potentially trigger 20–30 events, resulting in 20–30 re-renders. This would cause many UI frames to be dropped, causing elements to jump across the screen unexpectedly, instead of a transitioning across in a fluid motion.
To avoid this, first, we are going to track the animated style value coming from our gesture event as an Animated.Value.
Next, when the onGestureEvent callback is fired, we use the setValue method from the Animate.Value instance to modify the native node directly.
This allows the box to be dragged up and down along the y-axis fluidly without dropping any frames. All without re-rendering the component.