From da8ae011bbabc8acb7ef7f6903f68dd60aaa1f9d Mon Sep 17 00:00:00 2001 From: Eli White Date: Wed, 2 Oct 2019 11:26:24 -0700 Subject: [PATCH] Fix exception in scrollResponderScrollNativeHandleToKeyboard when ref is null Summary: We are seeing these errors in prod: ``` TypeError: Cannot read property '_nativeTag' of null at ReactNativeFiberHostComponent.prototype.measureLayout(ReactNativeRenderer-prod.fb.js:1594) ScrollResponderMixin.scrollResponderScrollNativeHandleToKeyboard(ScrollResponder.js:557) ``` This error is coming from these lines: https://github.com/facebook/react-native/blob/69c38e5a639f34620038ae5724426c92c355e509/Libraries/Components/ScrollResponder.js#L563-L567 Either `nodeHandle` is null or `this.getInnerViewRef()`. If `nodeHandle` was null, we'd get an error that we can't call `measureLayout` on null. So `this.getInnerViewRef()` must be null. In the React Native Renderer this error of `_nativeTag of null` is coming from this line: https://github.com/facebook/react/blob/db8afe4f6318dba422177a2054204ef089570ad8/packages/react-native-renderer/src/ReactNativeFiberHostComponent.js#L84 Which means indeed `this.getInnerViewRef()` is null. So adding a null check here which is what we do at all the other product callsites of `measureLayout`. Flow should have caught this, but because ScrollView is one of the only components left using mixins (ScrollResponder), `this.getInnerViewRef` is typed as `any` instead of what it should be: ``` ?React.ElementRef>> ``` If `scrollResponder` was typed correctly then Flow would have caught this. Changelog: [Fixed] Exception in scrollResponderScrollNativeHandleToKeyboard when ref is null Reviewed By: mmmulani Differential Revision: D17717150 fbshipit-source-id: d7bc4c897ad259fb588e8100f37ccfb8a5d07874 --- Libraries/Components/ScrollResponder.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/ScrollResponder.js b/Libraries/Components/ScrollResponder.js index f7da4efeae86cc..449f73ba350c5c 100644 --- a/Libraries/Components/ScrollResponder.js +++ b/Libraries/Components/ScrollResponder.js @@ -560,8 +560,14 @@ const ScrollResponderMixin = { this.scrollResponderInputMeasureAndScrollToKeyboard, ); } else { + const innerRef = this.getInnerViewRef(); + + if (innerRef == null) { + return; + } + nodeHandle.measureLayout( - this.getInnerViewRef(), + innerRef, this.scrollResponderInputMeasureAndScrollToKeyboard, this.scrollResponderTextInputFocusError, );