From 2c6cb27f88f418b4fdecaf592484ac14b67dcca2 Mon Sep 17 00:00:00 2001 From: generatedunixname1608173377072046 Date: Wed, 13 May 2026 17:53:57 -0700 Subject: [PATCH] Add null guard for changedTouches in ResponderTouchHistoryStore.recordTouchTrack (#56819) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56819 The native iOS touch system can occasionally dispatch touch events to JavaScript where `nativeEvent.changedTouches` is undefined due to inconsistencies between local and UIKit touch registries. This causes a TypeError crash in `ResponderTouchHistoryStore.recordTouchTrack` when it unconditionally calls `.forEach()` on `changedTouches`. The fix adds a defensive null check: if `changedTouches` is null/undefined, we log a DEV warning and bail out early from `recordTouchTrack`. Additionally, `nativeEvent.touches` accesses use optional chaining to prevent secondary crashes. Changelog: [iOS][Fixed] - Fix TypeError crash in ResponderTouchHistoryStore when changedTouches is undefined Reviewed By: fkgozali Differential Revision: D105023000 fbshipit-source-id: d3ade24e93f45614b094932e436f7190c8c0a985 --- .../renderer/events/ResponderTouchHistoryStore.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/react-native/src/private/renderer/events/ResponderTouchHistoryStore.js b/packages/react-native/src/private/renderer/events/ResponderTouchHistoryStore.js index c0b4d12fc7f..19d06b9bb5e 100644 --- a/packages/react-native/src/private/renderer/events/ResponderTouchHistoryStore.js +++ b/packages/react-native/src/private/renderer/events/ResponderTouchHistoryStore.js @@ -221,11 +221,20 @@ const ResponderTouchHistoryStore = { instrumentationCallback(topLevelType, nativeEvent); } + if (nativeEvent.changedTouches == null) { + console.error( + 'Touch event "%s" received with no changedTouches. This is likely ' + + 'a bug in the native touch handling system.', + topLevelType, + ); + return; + } + if (isMoveish(topLevelType)) { nativeEvent.changedTouches.forEach(recordTouchMove); } else if (isStartish(topLevelType)) { nativeEvent.changedTouches.forEach(recordTouchStart); - touchHistory.numberActiveTouches = nativeEvent.touches.length; + touchHistory.numberActiveTouches = nativeEvent.touches?.length ?? 0; if (touchHistory.numberActiveTouches === 1) { touchHistory.indexOfSingleActiveTouch = // $FlowFixMe[incompatible-type] might be null according to type @@ -233,7 +242,7 @@ const ResponderTouchHistoryStore = { } } else if (isEndish(topLevelType)) { nativeEvent.changedTouches.forEach(recordTouchEnd); - touchHistory.numberActiveTouches = nativeEvent.touches.length; + touchHistory.numberActiveTouches = nativeEvent.touches?.length ?? 0; if (touchHistory.numberActiveTouches === 1) { for (let i = 0; i < touchBank.length; i++) { const touchTrackToCheck = touchBank[i];