From b3af384a13e7e1d3ed4527b5455a0be708f6f00f Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 20 Jan 2020 01:28:55 -0800 Subject: [PATCH] Fix wrong layoutMetrics being returned by `measure` call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: # Logs ``` [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 0 tag: 1432 debugValue: r1 [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r2 [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r2 [tid:com.facebook.react.JavaScript][UIManager.cpp:197] width: 0 tag: 1432 debugValue: r1/sealed <----------------- FIRST TAP [tid:com.facebook.react.JavaScript][UIManager.cpp:197] width: 0 tag: 1432 debugValue: r1/sealed <----------------- FIRST TAP [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r4 [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r5 [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r6 [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r7 [tid:com.facebook.react.JavaScript][ConcreteViewShadowNode.h:119] width: 375 tag: 1432 debugValue: r8 [tid:com.facebook.react.JavaScript][UIManager.cpp:197] width: 375 tag: 1432 debugValue: r7/sealed <----------------- SECOND TAP [tid:com.facebook.react.JavaScript][UIManager.cpp:197] width: 375 tag: 1432 debugValue: r7/sealed <----------------- SECOND TAP ``` # What’s happening here? Opening a *BottomSheet* and tapping the first item. As you can see before the item is tapped, it has *width* set to 375 in revision *r2*. When the tap happens, JavaScript is requesting an old revision of ShadowNode, which still has width 0. # My assumption. 1. Native creates node with *width* 0 and returns handle to *JS*. 2. Native *clones* the node, sets its *width* to 375, doesn’t tell *JS* about it. This update is due to state change. 3. *JS* tries to get the size of the node, but asking for first revision of the component it receives width 0. # Other observations 1. Manually setting width to 375 in UIManager::getRelativeLayoutMetrics fixes the problem. 2. This happens only on device, I wasn’t able to reproduce this on simulator. # Fix Find the newest revision of ShadowNodeFamily, and return its layoutMetrics. Changelog: [Internal] Reviewed By: shergin Differential Revision: D19433873 fbshipit-source-id: 4558cf6e704051e9b3968e83821d8d25b3dadcda --- .../core/layout/LayoutableShadowNode.cpp | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp index b681634887f..d7ddc2688f2 100644 --- a/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp +++ b/ReactCommon/fabric/core/layout/LayoutableShadowNode.cpp @@ -17,6 +17,22 @@ namespace facebook { namespace react { +/* + * `shadowNode` might not be the newest revision of `ShadowNodeFamily`. + * This function looks at `parentNode`'s children and finds one that belongs + * to the same family as `shadowNode`. + */ +static ShadowNode const *findNewestChildInParent( + ShadowNode const &parentNode, + ShadowNode const &shadowNode) { + for (auto const &child : parentNode.getChildren()) { + if (ShadowNode::sameFamily(*child, shadowNode)) { + return child.get(); + } + } + return nullptr; +} + LayoutMetrics LayoutableShadowNode::getLayoutMetrics() const { return layoutMetrics_; } @@ -53,7 +69,15 @@ LayoutMetrics LayoutableShadowNode::getRelativeLayoutMetrics( return EmptyLayoutMetrics; } - auto layoutMetrics = getLayoutMetrics(); + auto newestChild = + findNewestChildInParent(ancestors.rbegin()->first.get(), shadowNode); + + if (!newestChild) { + return EmptyLayoutMetrics; + } + + auto layoutMetrics = dynamic_cast(newestChild) + ->getLayoutMetrics(); // `AncestorList` starts from the given ancestor node and ends with the parent // node. We iterate from parent node (reverse iteration) and stop before the