Repository navigation
[iOS] Handle view recycling in Touchable - #4131
Merged
j-piasecki merged 2 commits intoApr 30, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes incorrect visual defaults when Touchable/native button views are reused under Fabric view recycling, ensuring recycled instances return to a clean animation/press state and re-apply default visual props correctly.
Changes:
- Add explicit recycle-time reset hooks for the Fabric button component view and the underlying native button.
- Force a one-shot re-application of the idle (“start animation”) state after recycling to ensure
default*props are applied. - Preserve wrapper-layer opacity across
finalizeUpdatesto avoid React re-renders clobbering press/idle visuals.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| packages/react-native-gesture-handler/apple/RNGestureHandlerButtonComponentView.mm | Adds prepareForRecycle, tracks recycle state to re-apply idle visuals, and snapshots/restores wrapper-layer opacity in finalizeUpdates. |
| packages/react-native-gesture-handler/apple/RNGestureHandlerButton.mm | Adds prepareForRecycle to cancel pending animations/blocks and reset transient press visual state (transform/alpha/underlay). |
| packages/react-native-gesture-handler/apple/RNGestureHandlerButton.h | Exposes prepareForRecycle API with documentation for Fabric recycling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
m-bert
approved these changes
Apr 29, 2026
j-piasecki
deleted the
@jpiasecki/ios-fix-touchable-default-props-recycling
branch
April 30, 2026 05:24
m-bert
added a commit
that referenced
this pull request
Jul 31, 2026
…cycling buttons (#4360) ## Description On Fabric, a v3 `Pressable` (or any `RNGestureHandlerButton`) with a style opacity rendered correctly on first mount, but lost the opacity once the component was unmounted and mounted again - the remount reuses a recycled native view and the button renders fully opaque. The root cause is a desync between the wrapper's layer and its retained props, introduced by the recycle reset from #4131: - `RNGestureHandlerButton.prepareForRecycle` resets `target.alpha = 1.0` to clear residual press-animation state, but target is the `animationTarget` - the wrapper `RNGestureHandlerButtonComponentView`, whose `layer.opacity` carries React's style opacity. - Fabric retains `_props` across recycling, and `RCTViewComponentView.updateProps`: re-applies opacity only when it differs from the retained props. - Even when another prop change triggers `invalidateLayer` (which would re-apply the correct opacity unconditionally), our `finalizeUpdates:` save/restore detects that as an unwanted change and restores the stale value. The fix re-syncs the layer from the retained props right after the button reset in `prepareForRecycle`, instead of leaving neutral values: `opacity` from `viewProps.opacity`, `transform` via `viewProps.resolveTransform(_layoutMetrics)`. > [!NOTE] > The transform gets the same treatment: the identity reset leaves the layer desynced from the retained props in the same way, it just isn't visible today because `updateLayoutMetrics` happens to recompute transforms after recycling. Re-syncing removes the reliance on that implementation detail. Fixes #4353 ## Test plan <details> <summary>Tested on the following code:</summary> ```tsx import React, { useState } from 'react'; import { Button, StyleSheet, Text, View } from 'react-native'; import { Pressable } from 'react-native-gesture-handler'; // Repro for #4353 // iOS + Fabric: style opacity (and transform) on a v3 Pressable is applied on // the first mount, but lost after the component is unmounted and mounted again // (the remount reuses a recycled native view). The plain Views on the right // show the expected rendering and should stay identical to the Pressables // after every toggle. export default function EmptyExample() { const [mounted, setMounted] = useState(true); const [generation, setGeneration] = useState(0); return ( <View style={styles.container}> <Button title={mounted ? 'Unmount' : 'Mount again'} onPress={() => { setMounted(!mounted); if (!mounted) { setGeneration(generation + 1); } }} /> <Text style={styles.caption}> Remounts: {generation} — left column: Pressable, right column: expected (plain View) </Text> <View style={styles.row}> {mounted ? ( <Pressable style={[styles.box, styles.opacityStyle]} onPress={() => console.log('press (opacity)')}> <Text style={styles.label}>opacity: 0.3</Text> </Pressable> ) : ( <View style={styles.boxPlaceholder} /> )} <View style={[styles.box, styles.opacityStyle]}> <Text style={styles.label}>opacity: 0.3</Text> </View> </View> <View style={styles.row}> {mounted ? ( <Pressable style={[styles.box, styles.transformStyle]} onPress={() => console.log('press (transform)')}> <Text style={styles.label}>rotate: 25deg</Text> </Pressable> ) : ( <View style={styles.boxPlaceholder} /> )} <View style={[styles.box, styles.transformStyle]}> <Text style={styles.label}>rotate: 25deg</Text> </View> </View> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', gap: 24, }, row: { flexDirection: 'row', gap: 48, }, box: { width: 120, height: 120, backgroundColor: 'mediumpurple', justifyContent: 'center', alignItems: 'center', }, boxPlaceholder: { width: 120, height: 120, }, opacityStyle: { opacity: 0.3, }, transformStyle: { transform: [{ rotate: '25deg' }], }, caption: { marginHorizontal: 24, textAlign: 'center', opacity: 0.5, }, label: { color: 'white', }, }); ``` </details>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Due to the Fabric view recycling not being handled correctly by the native button implementations, the
default*props were not applied correctly when the button instances were being reused. This presented itself by buttons being styled correctly on the first mount, but not during the following ones.This PR addresses that.
Test plan
Simulator.Screen.Recording.-.iPhone.17.Pro.-.2026-04-29.at.12.02.29.mov
Simulator.Screen.Recording.-.iPhone.17.Pro.-.2026-04-29.at.12.01.17.mov