From 21dff39ad661cf842cf9bb82a337a76a7f01829d Mon Sep 17 00:00:00 2001 From: Gregory Moskaliuk Date: Wed, 2 Dec 2026 20:22:10 +0100 Subject: [PATCH] Keep a node's layout when React appends children that lay out the same React's Fabric renderer rebuilds the children of an updated host node by cloning the node without children and appending them one by one, unless `passChildrenWhenCloningPersistedNodes` is on, which it is not in the open source renderer. `YogaLayoutableShadowNode::appendChild` always dirtied the node, so every update, including a paint-only one such as a background color change, dirtied every node from the updated one to the root, and Yoga laid out that path again. Behind the new `keepLayoutWhenAppendingUnchangedChildren` flag, a node cloned from a clean node with an empty list of children keeps the source's Yoga children, and `appendChild` compares each appended child with the source child at the same index using the checks `updateYogaChildren` applies when children are passed into the clone: the child is clean and has the same style and layout results. The node is dirtied when a child differs, or when a different number of children has been appended by the time a parent takes the node, a layout pass reads it, or the node is cloned. Co-Authored-By: Claude Opus 5.5 --- ...outWhenAppendingUnchangedChildren-itest.js | 218 +++++++++++++++++ .../featureflags/ReactNativeFeatureFlags.kt | 8 +- .../ReactNativeFeatureFlagsCxxAccessor.kt | 12 +- .../ReactNativeFeatureFlagsCxxInterop.kt | 4 +- .../ReactNativeFeatureFlagsDefaults.kt | 4 +- .../ReactNativeFeatureFlagsLocalAccessor.kt | 13 +- .../ReactNativeFeatureFlagsProvider.kt | 4 +- .../JReactNativeFeatureFlagsCxxInterop.cpp | 16 +- .../JReactNativeFeatureFlagsCxxInterop.h | 5 +- .../featureflags/ReactNativeFeatureFlags.cpp | 6 +- .../featureflags/ReactNativeFeatureFlags.h | 7 +- .../ReactNativeFeatureFlagsAccessor.cpp | 80 +++--- .../ReactNativeFeatureFlagsAccessor.h | 6 +- .../ReactNativeFeatureFlagsDefaults.h | 6 +- .../ReactNativeFeatureFlagsDynamicProvider.h | 11 +- .../ReactNativeFeatureFlagsProvider.h | 3 +- .../NativeReactNativeFeatureFlags.cpp | 7 +- .../NativeReactNativeFeatureFlags.h | 4 +- .../view/YogaLayoutableShadowNode.cpp | 130 ++++++++-- .../view/YogaLayoutableShadowNode.h | 18 ++ .../components/view/tests/ViewTest.cpp | 227 ++++++++++++++++++ .../core/__docs__/passChildrenWhenCloning.md | 11 + .../ReactNativeFeatureFlags.config.js | 11 + .../featureflags/ReactNativeFeatureFlags.js | 7 +- .../specs/NativeReactNativeFeatureFlags.js | 3 +- 25 files changed, 747 insertions(+), 74 deletions(-) create mode 100644 packages/react-native/Libraries/Components/View/__tests__/View-keepLayoutWhenAppendingUnchangedChildren-itest.js diff --git a/packages/react-native/Libraries/Components/View/__tests__/View-keepLayoutWhenAppendingUnchangedChildren-itest.js b/packages/react-native/Libraries/Components/View/__tests__/View-keepLayoutWhenAppendingUnchangedChildren-itest.js new file mode 100644 index 000000000000..6a564858b6d6 --- /dev/null +++ b/packages/react-native/Libraries/Components/View/__tests__/View-keepLayoutWhenAppendingUnchangedChildren-itest.js @@ -0,0 +1,218 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @fantom_flags keepLayoutWhenAppendingUnchangedChildren:* + * @fantom_flags updateRuntimeShadowNodeReferencesOnCommit:* + * @flow strict-local + * @format + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import type {HostInstance} from 'react-native'; + +import * as Fantom from '@react-native/fantom'; +import * as React from 'react'; +import {Text, View} from 'react-native'; + +// React rebuilds the children of an updated node by cloning it without +// children and appending them one by one. With +// `keepLayoutWhenAppendingUnchangedChildren`, the clone stays clean when the +// appended children lay out as before. These tests run with the flag off and +// on, render a sequence of updates, and check that the layout matches a fresh +// render of the last update. + +// `null` for a ref whose element is not rendered in the last update. +function rectOf(ref: { + current: HostInstance | null, +}): ?[number, number, number, number] { + if (ref.current == null) { + return null; + } + const rect = ref.current.getBoundingClientRect(); + return [rect.x, rect.y, rect.width, rect.height]; +} + +type Refs = ReadonlyArray>; + +function createRefs(count: number): Refs { + return Array.from({length: count}, () => React.createRef()); +} + +function expectSameLayoutAsFreshRender( + render: (step: number, refs: Refs) => React.MixedElement, + stepCount: number, + refCount: number, +): void { + const refs = createRefs(refCount); + const root = Fantom.createRoot(); + for (let step = 0; step < stepCount; step++) { + Fantom.runTask(() => { + root.render(render(step, refs)); + }); + } + + const freshRefs = createRefs(refCount); + const freshRoot = Fantom.createRoot(); + Fantom.runTask(() => { + freshRoot.render(render(stepCount - 1, freshRefs)); + }); + + expect(refs.map(rectOf)).toEqual(freshRefs.map(rectOf)); + + root.destroy(); + freshRoot.destroy(); +} + +// A row of `count` boxes inside two levels of containers, so that a change to +// one box rebuilds the children of every container above it. +function Boxes({ + count, + colorIndex, + wideIndex, + hidden, + refs, +}: { + count: number, + colorIndex: number, + wideIndex: number, + hidden?: boolean, + refs: Refs, +}): React.MixedElement { + return ( + +