Pressability: Setup w/ Insertion Effect (#55060)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55060

Ships the feature flag to use `useInsertionEffect` in `Pressability`, instead of `useEffect`.

Using `useInsertionEffect` enables `Pressability` to behave more predictability in component trees with `<Activity mode="hidden">` because the events are scheduled more similarly to platform controls (e.g. focus and blur events will still fire even when "hidden").

Changelog:
[General][Changed] - `Pressable` no longer unmounts event listeners in a hidden `Activity`.

Reviewed By: lunaleaps, javache

Differential Revision: D90192717

fbshipit-source-id: 845531c88cfa1984c8f4511f69e6436f9ba59d81
This commit is contained in:
Tim Yung authored and meta-codesync[bot] committed 2026-01-06 14:45:33 -08:00
1 parent 162627af7c
commit c295ec2261
3 files changed
+4 -32

No files matched your search

@@ -8,26 +8,15 @@
* @format
*/
import * as ReactNativeFeatureFlags from '../../src/private/featureflags/ReactNativeFeatureFlags';
import Pressability, {
type EventHandlers,
type PressabilityConfig,
} from './Pressability';
import {useEffect, useInsertionEffect, useRef} from 'react';
import {useInsertionEffect, useRef} from 'react';
declare function usePressability(config: PressabilityConfig): EventHandlers;
declare function usePressability(config: null | void): null | EventHandlers;
// Experiments with using `useInsertionEffect` instead of `useEffect`, which
// changes whether `Pressability` is configured or reset when inm a hidden
// Activity. With `useInsertionEffect`, `Pressability` behaves more like a
// platform control (e.g. Pointer Events), especially with respect to events
// like focus and blur.
const useConfigurationEffect =
ReactNativeFeatureFlags.configurePressabilityDuringInsertion()
? useInsertionEffect
: useEffect;
/**
* Creates a persistent instance of `Pressability` that automatically configures
* itself and resets. Accepts null `config` to support lazy initialization. Once
@@ -51,7 +40,7 @@ export default function usePressability(
// On the initial mount, this is a no-op. On updates, `pressability` will be
// re-configured to use the new configuration.
useConfigurationEffect(() => {
useInsertionEffect(() => {
if (config != null && pressability != null) {
pressability.configure(config);
}
@@ -59,7 +48,7 @@ export default function usePressability(
// On unmount, reset pending state and timers inside `pressability`. This is
// a separate effect because we do not want to reset when `config` changes.
useConfigurationEffect(() => {
useInsertionEffect(() => {
if (pressability != null) {
return () => {
pressability.reset();
@@ -989,17 +989,6 @@ const definitions: FeatureFlagDefinitions = {
},
ossReleaseStage: 'none',
},
configurePressabilityDuringInsertion: {
defaultValue: false,
metadata: {
dateAdded: '2025-10-27',
description:
'Configure Pressability during insertion and no longer unmount when hidden.',
expectedReleaseValue: true,
purpose: 'experimentation',
},
ossReleaseStage: 'none',
},
deferFlatListFocusChangeRenderUpdate: {
defaultValue: false,
metadata: {
@@ -4,7 +4,7 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated SignedSource<<43e6a2dddd5965011ba166098f90e33f>>
* @generated SignedSource<<bf6ca0830458dd53c3d8edf227c1c4d9>>
* @flow strict
* @noformat
*/
@@ -31,7 +31,6 @@ export type ReactNativeFeatureFlagsJsOnly = $ReadOnly<{
jsOnlyTestFlag: Getter<boolean>,
animatedShouldDebounceQueueFlush: Getter<boolean>,
animatedShouldUseSingleOp: Getter<boolean>,
configurePressabilityDuringInsertion: Getter<boolean>,
deferFlatListFocusChangeRenderUpdate: Getter<boolean>,
disableMaintainVisibleContentPosition: Getter<boolean>,
enableVirtualViewExperimental: Getter<boolean>,
@@ -154,11 +153,6 @@ export const animatedShouldDebounceQueueFlush: Getter<boolean> = createJavaScrip
*/
export const animatedShouldUseSingleOp: Getter<boolean> = createJavaScriptFlagGetter('animatedShouldUseSingleOp', false);
/**
* Configure Pressability during insertion and no longer unmount when hidden.
*/
export const configurePressabilityDuringInsertion: Getter<boolean> = createJavaScriptFlagGetter('configurePressabilityDuringInsertion', false);
/**
* Use the deferred cell render update mechanism for focus change in FlatList.
*/