[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
Joshua Gross committed
Sep 24, 2019 at 17:46 UTC
32e5c97d11e390e6a3d3ce6a2ab7443daed09747
3 files changed
+28
-2
packages/react-native-renderer/src/__mocks__/react-native/Libraries/ReactPrivate/ReactNativeViewConfigRegistry.js
+8
-1
@@ -78,6 +78,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
78
'Tried to register two views with the same name %s',
79
name,
80
);
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
+ );
87
viewConfigCallbacks.set(name, callback);
88
return name;
89
};
@@ -94,8 +100,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
100
if (typeof callback !== 'function') {
101
invariant(
102
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',
104
name,
105
+ callback === null ? 'null' : typeof callback,
106
typeof name[0] === 'string' && /[a-z]/.test(name[0])
107
? ' Make sure to start component names with a capital letter.'
108
: '',
packages/react-native-renderer/src/__tests__/ReactNativeError-test.internal.js
+12
@@ -32,6 +32,18 @@ describe('ReactNativeError', () => {
32
.computeComponentStackForErrorReporting;
33
});
34
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
+
47
it('should be able to extract a component stack from a native view', () => {
48
const View = createReactNativeComponentClass('View', () => ({
49
validAttributes: {foo: true},
scripts/rollup/shims/react-native/ReactNativeViewConfigRegistry.js
+8
-1
@@ -75,6 +75,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
75
'Tried to register two views with the same name %s',
76
name,
77
);
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
+ );
84
viewConfigCallbacks.set(name, callback);
85
return name;
86
};
@@ -91,8 +97,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
97
if (typeof callback !== 'function') {
98
invariant(
99
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',
101
name,
102
+ callback === null ? 'null' : typeof callback,
103
typeof name[0] === 'string' && /[a-z]/.test(name[0])
104
? ' Make sure to start component names with a capital letter.'
105
: '',