From c31f08b8831959f7f90c85702217b621e4025663 Mon Sep 17 00:00:00 2001 From: Riccardo Cipolleschi Date: Wed, 18 Sep 2024 09:08:10 -0700 Subject: [PATCH] Fix crash when navigating away from screens (#46559) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/46559 There is an edge case when we navigate away from a screen that contains a scroll view where one of the UISCrollViewDelegates does not implement the scrollViewDidEndDecelerating method. This happens because the Macro used assumes that the event that we are forwarding is the actual method from where the macro is called. Which is not true when it comes to `didMoveToWindow`. This change fixes that by explicitly expanding the macro in this scenario and passing the right selector. ## Changelog: [iOS][Fixed] - Fixed a crash when navigating away from a screen that contains a scrollView ## Facebook This should fix T201780472 Differential Revision: D62935876 --- .../react-native/React/Views/ScrollView/RCTScrollView.m | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/react-native/React/Views/ScrollView/RCTScrollView.m b/packages/react-native/React/Views/ScrollView/RCTScrollView.m index 843bbd7cf859ff..e0dcc3b4a3b4a6 100644 --- a/packages/react-native/React/Views/ScrollView/RCTScrollView.m +++ b/packages/react-native/React/Views/ScrollView/RCTScrollView.m @@ -856,7 +856,14 @@ - (void)didMoveToWindow if (_scrollView.isDecelerating || !_scrollView.isTracking) { // Trigger the onMomentumScrollEnd event manually RCT_SEND_SCROLL_EVENT(onMomentumScrollEnd, nil); - RCT_FORWARD_SCROLL_EVENT(scrollViewDidEndDecelerating : _scrollView); + // We can't use the RCT_FORWARD_SCROLL_EVENT here beacuse the `_cmd` parameter passed + // to `respondsToSelector` is the current method - so it will be `didMoveToWindow` - and not + // `scrollViewDidEndDecelerating` that is passed. + for (NSObject *scrollViewListener in _scrollListeners) { + if ([scrollViewListener respondsToSelector:@selector(scrollViewDidEndDecelerating:)]) { + [scrollViewListener performSelector:@selector(scrollViewDidEndDecelerating:) withObject:_scrollView]; + } + } } } }