Add useHandleRef as a lighter alternative for useAnimatedRef #6500
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We use
useAnimatedRef
in a bunch of places just to get the measurements withmeasure
. This is unfortunate because it contains two shared values, one of which stores an internal React node. I'm not sure whether that node goes through the serialization mechanism but the profiler was showing some overhead associated with eachuseAnimatedRef
component.In this PR, I'm taking just the part that we actually need — reading the component's internal handle. I'm doing the same thing
findNodeHandle
does except for the slow codepath we don't need (which only triggers for class components). I put that code inline so it's clearer how it works.On the
measure
side, I realized that all Reanimated'smeasure()
really wants is a function that returns the handle. (TheuseAnimatedRef
API plays a trick here where it doubles as both a React callback ref which receives the component and as a function thatmeasure
can call to get the value.) So I'm just passing() => handle
for that case.I've also replaced
<Animated.View>
with plain<View>
to reduce overhead since there's no need for Reanimated there at all.Test Plan
Verify you're still able to expand/collapse images:
AutoSizedImage
)handle.mov
andr.mov
Before
ImageLayoutGridInner
andAutoSizedImage
were sticking out as having a bunch of self-time in traces.After
ImageLayoutGridInner
andAutoSizedImage
no longer take disproportionate time in traces.