Skip to content

Commit 32e5c97

Browse files
JoshuaGrosselicwhite
authored andcommitted
[React Native] Improve errors for invalid ViewConfig getter functions (#16879)
* [React Native] Improve logging for missing view configs and invalid view config getter functions * [React Native] Improve logging for missing view configs and invalid view config getter functions
1 parent ebc299f commit 32e5c97

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
7878
'Tried to register two views with the same name %s',
7979
name,
8080
);
81+
invariant(
82+
typeof callback === 'function',
83+
'View config getter callback for component `%s` must be a function (received `%s`)',
84+
name,
85+
callback === null ? 'null' : typeof callback,
86+
);
8187
viewConfigCallbacks.set(name, callback);
8288
return name;
8389
};
@@ -94,8 +100,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
94100
if (typeof callback !== 'function') {
95101
invariant(
96102
false,
97-
'View config not found for name %s.%s',
103+
'View config getter callback for component `%s` must be a function (received `%s`).%s',
98104
name,
105+
callback === null ? 'null' : typeof callback,
99106
typeof name[0] === 'string' && /[a-z]/.test(name[0])
100107
? ' Make sure to start component names with a capital letter.'
101108
: '',

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ describe('ReactNativeError', () => {
3232
.computeComponentStackForErrorReporting;
3333
});
3434

35+
it('should throw error if null component registration getter is used', () => {
36+
expect(() => {
37+
try {
38+
createReactNativeComponentClass('View', null);
39+
} catch (e) {
40+
throw new Error(e.toString());
41+
}
42+
}).toThrow(
43+
'Invariant Violation: View config getter callback for component `View` must be a function (received `null`)',
44+
);
45+
});
46+
3547
it('should be able to extract a component stack from a native view', () => {
3648
const View = createReactNativeComponentClass('View', () => ({
3749
validAttributes: {foo: true},

scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
7575
'Tried to register two views with the same name %s',
7676
name,
7777
);
78+
invariant(
79+
typeof callback === 'function',
80+
'View config getter callback for component `%s` must be a function (received `%s`)',
81+
name,
82+
callback === null ? 'null' : typeof callback,
83+
);
7884
viewConfigCallbacks.set(name, callback);
7985
return name;
8086
};
@@ -91,8 +97,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
9197
if (typeof callback !== 'function') {
9298
invariant(
9399
false,
94-
'View config not found for name %s.%s',
100+
'View config getter callback for component `%s` must be a function (received `%s`).%s',
95101
name,
102+
callback === null ? 'null' : typeof callback,
96103
typeof name[0] === 'string' && /[a-z]/.test(name[0])
97104
? ' Make sure to start component names with a capital letter.'
98105
: '',

0 commit comments

Comments
 (0)