From 68db74205afdd190304eb73ef71710781fa580b9 Mon Sep 17 00:00:00 2001 From: Matthew Horan Date: Thu, 21 Nov 2024 12:10:17 -0800 Subject: [PATCH] Fix asynchronous access to KeyboardAvoidingEvent event in onLayout handler (#47798) Summary: The `onLayout` handler of the `KeyboardAvoidingView` view is async due to the `await AccessibilityInfo.prefersCrossFadeTransitions` in `_relativeKeyboardHeight`. A connected `onLayout` handler will be unable to access the event. Call `persist()` on the event so that it can be accessed asynchronously. ## Changelog: [GENERAL] [FIXED] - Accessing KeyboardAvoidingEvent event in onLayout handler Pull Request resolved: https://github.com/facebook/react-native/pull/47798 Test Plan: The following should not produce an error: ``` import * as React from 'react'; import { KeyboardAvoidingView, SafeAreaView, TextInput } from 'react-native'; export default function() { return ( console.log(event)} behavior="padding"> ); } ``` Reviewed By: cipolleschi Differential Revision: D66306085 Pulled By: dmytrorykun fbshipit-source-id: 9ccf00db79947670ddc7de74a47cc6dc27455fc0 --- .../Libraries/Components/Keyboard/KeyboardAvoidingView.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js b/packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js index 3a3f101c8d9..77c81f04316 100644 --- a/packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js +++ b/packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js @@ -116,6 +116,8 @@ class KeyboardAvoidingView extends React.Component { }; _onLayout = async (event: ViewLayoutEvent) => { + event.persist(); + const oldFrame = this._frame; this._frame = event.nativeEvent.layout; if (!this._initialFrameHeight) {