From 7a1679f6a9975b701ed1ef096de10ac62b0f6d0b Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 30 Nov 2026 11:36:41 +0000 Subject: [PATCH] Make setNativeProps from JS visible to later reads in the same task `setNativeProps` commits synchronously, but JS layout reads use the revision captured for the task, and only React's commits refresh it. So a read, then `setNativeProps`, then a read in the same task returns the old layout. JS-driven Animated on Fabric applies its values with `setNativeProps`, so it's affected the same way. `ResizeObserver` gathers against the same revision, so a resize that a callback makes with `setNativeProps` isn't delivered for a deeper target until the surface next commits. This adds an overload of `setNativeProps_DEPRECATED` for JS, which refreshes the captured revision after a successful commit, as `completeSurface` does. It takes the runtime only to ensure it runs on the JS thread, as `EventTarget::release` does. Both JS entry points, `NativeDOM` and `nativeFabricUIManager`, use it. The existing overload, which nothing else in this repo calls, keeps its behavior for native callers, so a call from another thread still can't change what JS sees partway through a task. It now returns the commit status, so that a cancelled commit doesn't refresh anything. A `ResizeObserver` callback that resizes an observed target no deeper than the shallowest one delivered in that round, such as its own target, now gets the spec's loop error, logged with `console.error`. When that target is delivered doesn't change: it still waits for the surface's next commit. With commit branching and a React revision pending, later reads and `ResizeObserver` see React's revision, which doesn't include the call. Neither does the tree that the merge mounts, until the node is next cloned and its stored props are applied again. Changelog: [General][Fixed] - Layout reads see earlier `setNativeProps` calls in the same task, including JS-driven Animated updates --- .../react/nativemodule/dom/NativeDOM.cpp | 2 +- .../react/renderer/uimanager/UIManager.cpp | 19 ++++- .../react/renderer/uimanager/UIManager.h | 14 +++- .../renderer/uimanager/UIManagerBinding.cpp | 1 + ...LazyShadowTreeRevisionConsistencyManager.h | 4 +- .../__tests__/UIConsistency-itest.js | 31 +++++++- .../__tests__/ResizeObserver-itest.js | 75 +++++++++++++++++++ 7 files changed, 139 insertions(+), 7 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.cpp b/packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.cpp index 3a24d57807e3..2402c228dbe2 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.cpp @@ -484,7 +484,7 @@ void NativeDOM::setNativeProps( std::shared_ptr shadowNode, jsi::Value updatePayload) { getUIManagerFromRuntime(rt).setNativeProps_DEPRECATED( - shadowNode, RawProps(rt, updatePayload)); + rt, shadowNode, RawProps(rt, updatePayload)); } } // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp index 6a18881fd844..0eab154225fe 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.cpp @@ -460,7 +460,7 @@ void UIManager::dispatchCommand( } } -void UIManager::setNativeProps_DEPRECATED( +ShadowTree::CommitStatus UIManager::setNativeProps_DEPRECATED( const std::shared_ptr& shadowNode, RawProps rawProps) const { auto& family = shadowNode->getFamily(); @@ -484,13 +484,14 @@ void UIManager::setNativeProps_DEPRECATED( } } + auto commitStatus = ShadowTree::CommitStatus::Cancelled; shadowTreeRegistry_.visit( family.getSurfaceId(), [&](const ShadowTree& shadowTree) { // The lambda passed to `commit` may be executed multiple times. // We need to create fresh copy of the `RawProps` object each time. auto ancestorShadowNode = shadowTree.getCurrentRevision().rootShadowNode; - shadowTree.commit( + commitStatus = shadowTree.commit( [&](const RootShadowNode& oldRootShadowNode) { auto rootNode = oldRootShadowNode.cloneTree( family, [&](const ShadowNode& oldShadowNode) { @@ -512,6 +513,20 @@ void UIManager::setNativeProps_DEPRECATED( }, {/* default commit options */}); }); + return commitStatus; +} + +void UIManager::setNativeProps_DEPRECATED( + jsi::Runtime& /*runtime*/, + const std::shared_ptr& shadowNode, + RawProps rawProps) const { + if (setNativeProps_DEPRECATED(shadowNode, std::move(rawProps)) == + ShadowTree::CommitStatus::Succeeded) { + // As `completeSurface` does, so that reads later in the task see these + // props even if the task read this surface before the call. + lazyShadowTreeRevisionConsistencyManager_->updateCurrentRevision( + shadowNode->getSurfaceId()); + } } void UIManager::sendAccessibilityEvent( diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h index 541d57a0b258..19c054e0169b 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManager.h @@ -202,7 +202,19 @@ class UIManager final : public ShadowTreeDelegate { const std::string &commandName, const folly::dynamic &args) const; - void setNativeProps_DEPRECATED(const std::shared_ptr &shadowNode, RawProps rawProps) const; + ShadowTree::CommitStatus setNativeProps_DEPRECATED( + const std::shared_ptr &shadowNode, + RawProps rawProps) const; + + /* + * `setNativeProps` called from JS, which takes the runtime only to ensure it + * runs on the JS thread. Refreshes the revision that JS reads, as + * `completeSurface` does. + */ + void setNativeProps_DEPRECATED( + jsi::Runtime &runtime, + const std::shared_ptr &shadowNode, + RawProps rawProps) const; void sendAccessibilityEvent(const std::shared_ptr &shadowNode, const std::string &eventType); diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp index a3fcfa76cf81..e2eeeb852187 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp @@ -604,6 +604,7 @@ jsi::Value UIManagerBinding::get( validateArgumentCount(runtime, methodName, paramCount, count); uiManager->setNativeProps_DEPRECATED( + runtime, Bridging>::fromJs( runtime, arguments[0]), RawProps(runtime, arguments[1])); diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/consistency/LazyShadowTreeRevisionConsistencyManager.h b/packages/react-native/ReactCommon/react/renderer/uimanager/consistency/LazyShadowTreeRevisionConsistencyManager.h index 8be01888d67a..69fb67119048 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/consistency/LazyShadowTreeRevisionConsistencyManager.h +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/consistency/LazyShadowTreeRevisionConsistencyManager.h @@ -21,8 +21,8 @@ namespace facebook::react { /** * This class implements UI consistency for the JavaScript thread. * This implementation forces JavaScript to see a stable revision of the shadow - * tree for a given surface ID, only updating it when React commits a new tree - * or between JS tasks. + * tree for a given surface ID, only updating it when JS commits a new tree or + * between JS tasks. */ class LazyShadowTreeRevisionConsistencyManager : public ShadowTreeRevisionConsistencyManager, public ShadowTreeRevisionProvider { diff --git a/packages/react-native/src/private/renderer/consistency/__tests__/UIConsistency-itest.js b/packages/react-native/src/private/renderer/consistency/__tests__/UIConsistency-itest.js index d05c9d0b6ac0..25b388132ef9 100644 --- a/packages/react-native/src/private/renderer/consistency/__tests__/UIConsistency-itest.js +++ b/packages/react-native/src/private/renderer/consistency/__tests__/UIConsistency-itest.js @@ -13,11 +13,15 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; import type {HostInstance} from 'react-native'; +import {getFabricUIManager} from '../../../../../Libraries/ReactNative/FabricUIManager'; import * as Fantom from '@react-native/fantom'; import nullthrows from 'nullthrows'; import * as React from 'react'; import {useLayoutEffect} from 'react'; -import {ScrollView, Text} from 'react-native'; +import {ScrollView, Text, View} from 'react-native'; +import {getNodeFromPublicInstance} from 'react-native/react-private-interface'; + +const UIManager = nullthrows(getFabricUIManager()); describe('UIConsistency', () => { it('should provide consistent data from the tree within the same synchronous function', () => { @@ -146,4 +150,29 @@ describe('UIConsistency', () => { ); }); }); + + it('should provide up-to-date data after setNativeProps in the same task', () => { + const root = Fantom.createRoot(); + + const viewRef = React.createRef(); + + Fantom.runTask(() => { + root.render(); + }); + + const view = nullthrows(viewRef.current); + const widths: Array = []; + + Fantom.runTask(() => { + widths.push(view.getBoundingClientRect().width); + view.setNativeProps({style: {width: 100}}); + widths.push(view.getBoundingClientRect().width); + UIManager.setNativeProps(nullthrows(getNodeFromPublicInstance(view)), { + width: 150, + }); + widths.push(view.getBoundingClientRect().width); + }); + + expect(widths).toEqual([50, 100, 150]); + }); }); diff --git a/packages/react-native/src/private/webapis/resizeobserver/__tests__/ResizeObserver-itest.js b/packages/react-native/src/private/webapis/resizeobserver/__tests__/ResizeObserver-itest.js index 929cb39d0a5d..cd7160351cf0 100644 --- a/packages/react-native/src/private/webapis/resizeobserver/__tests__/ResizeObserver-itest.js +++ b/packages/react-native/src/private/webapis/resizeobserver/__tests__/ResizeObserver-itest.js @@ -2354,6 +2354,53 @@ describe('ResizeObserver', () => { const [secondEntries] = callback.mock.calls[1]; expect(secondEntries.map(entry => entry.target)).toEqual([nodeB]); }); + + it('should deliver a deeper target resized with setNativeProps from a callback on a follow-up loop round', () => { + const parentRef = createRef(); + const childRef = createRef(); + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + const parent = nullthrows(parentRef.current); + const child = nullthrows(childRef.current); + const events: Array = []; + + Fantom.runTask(() => { + observer = new ResizeObserver(entries => { + for (const entry of entries) { + const width = entry.contentRect.width; + if (entry.target === parent) { + events.push(`parent ${width}`); + if (width === 120) { + child.setNativeProps({style: {width: 80}}); + } + } else { + events.push(`child ${width}`); + } + } + }); + observer.observe(parent); + observer.observe(child); + }); + expect(events).toEqual(['parent 100', 'child 50']); + + events.length = 0; + Fantom.runTask(() => { + parent.setNativeProps({style: {width: 120}}); + Fantom.scheduleTask(() => { + events.push('next task'); + }); + }); + + expect(events).toEqual(['parent 120', 'child 80', 'next task']); + }); }); describe('resize loop error', () => { @@ -2437,6 +2484,34 @@ describe('ResizeObserver', () => { expect(callback).toHaveBeenCalledTimes(2); expectNoResizeLoopError(); }); + + it('should report a loop error when a callback resizes its own target with setNativeProps', () => { + const nodeRef = createRef(); + const root = Fantom.createRoot(); + + Fantom.runTask(() => { + root.render(); + }); + + const node = nullthrows(nodeRef.current); + const callback: ResizeObserverMockCallback = jest.fn(entries => { + if (entries[0].contentRect.width === 100) { + node.setNativeProps({style: {width: 120}}); + } + }); + + Fantom.runTask(() => { + observer = new ResizeObserver(callback); + observer.observe(node); + }); + + // The new size is at the depth just delivered, so it's skipped. + expect(callback).toHaveBeenCalledTimes(1); + expect(consoleErrorMock).toHaveBeenCalledTimes(1); + expect(consoleErrorMock.mock.lastCall[0]).toBe( + 'ResizeObserver loop completed with undelivered notifications.', + ); + }); }); describe('state updates scheduled from callbacks', () => {