Skip to content

Commit b14d7fa

Browse files
authored
Add support for setNativeProps to Fabric (#25737)
Add support for `setNativeProps` in Fabric to make migration to the new architecture easier. The React Native part of this has already landed in the core and iOS in 1d3fa40. It is still recommended to move away from `setNativeProps` because the API will not work with future features.
1 parent 1c7055d commit b14d7fa

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

packages/react-native-renderer/src/ReactFabricHostConfig.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import type {
1818
TouchedViewDataAtPoint,
1919
} from './ReactNativeTypes';
2020

21-
import {mountSafeCallback_NOT_REALLY_SAFE} from './NativeMethodsMixinUtils';
21+
import {
22+
mountSafeCallback_NOT_REALLY_SAFE,
23+
warnForStyleProps,
24+
} from './NativeMethodsMixinUtils';
2225
import {create, diff} from './ReactNativeAttributePayload';
2326

2427
import {dispatchEvent} from './ReactFabricEventEmitter';
@@ -52,6 +55,7 @@ const {
5255
unstable_DefaultEventPriority: FabricDefaultPriority,
5356
unstable_DiscreteEventPriority: FabricDiscretePriority,
5457
unstable_getCurrentEventPriority: fabricGetCurrentEventPriority,
58+
setNativeProps,
5559
} = nativeFabricUIManager;
5660

5761
const {get: getViewConfigForType} = ReactNativeViewConfigRegistry;
@@ -208,12 +212,14 @@ class ReactFabricHostComponent {
208212

209213
setNativeProps(nativeProps: Object) {
210214
if (__DEV__) {
211-
console.error(
212-
'Warning: setNativeProps is not currently supported in Fabric',
213-
);
215+
warnForStyleProps(nativeProps, this.viewConfig.validAttributes);
214216
}
217+
const updatePayload = create(nativeProps, this.viewConfig.validAttributes);
215218

216-
return;
219+
const {stateNode} = this._internalInstanceHandle;
220+
if (stateNode != null && updatePayload != null) {
221+
setNativeProps(stateNode.node, updatePayload);
222+
}
217223
}
218224

219225
// This API (addEventListener, removeEventListener) attempts to adhere to the

packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/InitializeNativeFabricUIManager.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ const RCTFabricUIManager = {
117117

118118
dispatchCommand: jest.fn(),
119119

120+
setNativeProps: jest.fn(),
121+
120122
sendAccessibilityEvent: jest.fn(),
121123

122124
registerEventHandler: jest.fn(function registerEventHandler(callback) {}),

packages/react-native-renderer/src/__tests__/ReactFabricHostComponent-test.internal.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function mockRenderKeys(keyLists) {
3838

3939
const mockContainerTag = 11;
4040
const MockView = createReactNativeComponentClass('RCTMockView', () => ({
41-
validAttributes: {},
41+
validAttributes: {foo: true},
4242
uiViewClassName: 'RCTMockView',
4343
}));
4444

@@ -200,21 +200,15 @@ describe('measureLayout', () => {
200200
});
201201

202202
describe('setNativeProps', () => {
203-
test('setNativeProps(...) emits a warning', () => {
203+
test('setNativeProps(...) invokes setNativeProps on Fabric UIManager', () => {
204204
const {
205205
UIManager,
206206
} = require('react-native/Libraries/ReactPrivate/ReactNativePrivateInterface');
207207

208208
const [[fooRef]] = mockRenderKeys([['foo']]);
209+
fooRef.setNativeProps({foo: 'baz'});
209210

210-
expect(() => {
211-
fooRef.setNativeProps({});
212-
}).toErrorDev(
213-
['Warning: setNativeProps is not currently supported in Fabric'],
214-
{
215-
withoutStack: true,
216-
},
217-
);
218211
expect(UIManager.updateView).not.toBeCalled();
212+
expect(nativeFabricUIManager.setNativeProps).toHaveBeenCalledTimes(1);
219213
});
220214
});

scripts/flow/react-native-host-hooks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ declare var nativeFabricUIManager: {
186186
payload: Object,
187187
) => void,
188188
) => void,
189-
189+
setNativeProps: (node: Object, nativeProps: Object) => Object,
190190
dispatchCommand: (node: Object, command: string, args: Array<any>) => void,
191191
sendAccessibilityEvent: (node: Object, eventTypeName: string) => void,
192192

0 commit comments

Comments
 (0)