From 7b916be03fa06b3a6328a7a3671f642707999e0c Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Mon, 20 Mar 2023 12:34:54 -0700 Subject: [PATCH] Patch setNativeProps in OSS build Summary: changelog: [internal] `setNativeProps` have been implemented in Fabric but require a React change to be accessible from JavaScript components. For React sync to be imported in OSS builds, React needs to have have a new release in NPM. To work around this limitation, I manually copied over lines of code that glue together setNativeProps to OSS build. Reviewed By: cortinico, cipolleschi Differential Revision: D44216496 fbshipit-source-id: b34b598c1a317bdc03cc5a4cadb3f7c610059709 --- .../implementations/ReactFabric-dev.js | 29 +++++++++++++++++-- .../implementations/ReactFabric-prod.js | 14 ++++++++- .../implementations/ReactFabric-profiling.js | 14 ++++++++- 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/packages/react-native/Libraries/Renderer/implementations/ReactFabric-dev.js b/packages/react-native/Libraries/Renderer/implementations/ReactFabric-dev.js index beb1a29161..2f47b2a238 100644 --- a/packages/react-native/Libraries/Renderer/implementations/ReactFabric-dev.js +++ b/packages/react-native/Libraries/Renderer/implementations/ReactFabric-dev.js @@ -3419,6 +3419,23 @@ function mountSafeCallback_NOT_REALLY_SAFE(context, callback) { return callback.apply(context, arguments); }; } +function warnForStyleProps(props, validAttributes) { + { + for (var key in validAttributes.style) { + if (!(validAttributes[key] || props[key] === undefined)) { + error( + "You are setting the style `{ %s" + + ": ... }` as a prop. You " + + "should nest it in a style object. " + + "E.g. `{ style: { %s" + + ": ... } }`", + key, + key + ); + } + } + } +} // Modules provided by RN: var emptyObject = {}; @@ -5100,7 +5117,8 @@ var _nativeFabricUIManage = nativeFabricUIManager, FabricDefaultPriority = _nativeFabricUIManage.unstable_DefaultEventPriority, FabricDiscretePriority = _nativeFabricUIManage.unstable_DiscreteEventPriority, fabricGetCurrentEventPriority = - _nativeFabricUIManage.unstable_getCurrentEventPriority; + _nativeFabricUIManage.unstable_getCurrentEventPriority, + _setNativeProps = _nativeFabricUIManage.setNativeProps; var getViewConfigForType = ReactNativePrivateInterface.ReactNativeViewConfigRegistry.get; // Counter for uniquely identifying views. // % 10 === 1 means it is a rootTag. @@ -5199,10 +5217,15 @@ var ReactFabricHostComponent = /*#__PURE__*/ (function() { _proto.setNativeProps = function setNativeProps(nativeProps) { { - error("Warning: setNativeProps is not currently supported in Fabric"); + warnForStyleProps(nativeProps, this.viewConfig.validAttributes); } - return; + var updatePayload = create(nativeProps, this.viewConfig.validAttributes); + var stateNode = this._internalInstanceHandle.stateNode; + + if (stateNode != null && updatePayload != null) { + _setNativeProps(stateNode.node, updatePayload); + } }; // This API (addEventListener, removeEventListener) attempts to adhere to the // w3 Level2 Events spec as much as possible, treating HostComponent as a DOM node. // diff --git a/packages/react-native/Libraries/Renderer/implementations/ReactFabric-prod.js b/packages/react-native/Libraries/Renderer/implementations/ReactFabric-prod.js index 7da25248f0..7821c743f2 100644 --- a/packages/react-native/Libraries/Renderer/implementations/ReactFabric-prod.js +++ b/packages/react-native/Libraries/Renderer/implementations/ReactFabric-prod.js @@ -1922,6 +1922,7 @@ var _nativeFabricUIManage = nativeFabricUIManager, FabricDiscretePriority = _nativeFabricUIManage.unstable_DiscreteEventPriority, fabricGetCurrentEventPriority = _nativeFabricUIManage.unstable_getCurrentEventPriority, + _setNativeProps = _nativeFabricUIManage.setNativeProps, getViewConfigForType = ReactNativePrivateInterface.ReactNativeViewConfigRegistry.get, nextReactTag = 2; @@ -1979,7 +1980,18 @@ var ReactFabricHostComponent = (function() { ); } }; - _proto.setNativeProps = function() {}; + _proto.setNativeProps = function(nativeProps) { + nativeProps = diffProperties( + null, + emptyObject, + nativeProps, + this.viewConfig.validAttributes + ); + var stateNode = this._internalInstanceHandle.stateNode; + null != stateNode && + null != nativeProps && + _setNativeProps(stateNode.node, nativeProps); + }; _proto.addEventListener_unstable = function(eventType, listener, options) { if ("string" !== typeof eventType) throw Error("addEventListener_unstable eventType must be a string"); diff --git a/packages/react-native/Libraries/Renderer/implementations/ReactFabric-profiling.js b/packages/react-native/Libraries/Renderer/implementations/ReactFabric-profiling.js index 9ea3a0f9de..b1185b2738 100644 --- a/packages/react-native/Libraries/Renderer/implementations/ReactFabric-profiling.js +++ b/packages/react-native/Libraries/Renderer/implementations/ReactFabric-profiling.js @@ -1981,6 +1981,7 @@ var _nativeFabricUIManage = nativeFabricUIManager, FabricDiscretePriority = _nativeFabricUIManage.unstable_DiscreteEventPriority, fabricGetCurrentEventPriority = _nativeFabricUIManage.unstable_getCurrentEventPriority, + _setNativeProps = _nativeFabricUIManage.setNativeProps, getViewConfigForType = ReactNativePrivateInterface.ReactNativeViewConfigRegistry.get, nextReactTag = 2; @@ -2038,7 +2039,18 @@ var ReactFabricHostComponent = (function() { ); } }; - _proto.setNativeProps = function() {}; + _proto.setNativeProps = function(nativeProps) { + nativeProps = diffProperties( + null, + emptyObject, + nativeProps, + this.viewConfig.validAttributes + ); + var stateNode = this._internalInstanceHandle.stateNode; + null != stateNode && + null != nativeProps && + _setNativeProps(stateNode.node, nativeProps); + }; _proto.addEventListener_unstable = function(eventType, listener, options) { if ("string" !== typeof eventType) throw Error("addEventListener_unstable eventType must be a string");