mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[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
This commit is contained in:
1 parent
ebc299fc2f
commit
32e5c97d11
3 files changed
+28
-2
No files matched your search
+8
-1
@@ -78,6 +78,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
|
||||
'Tried to register two views with the same name %s',
|
||||
name,
|
||||
);
|
||||
invariant(
|
||||
typeof callback === 'function',
|
||||
'View config getter callback for component `%s` must be a function (received `%s`)',
|
||||
name,
|
||||
callback === null ? 'null' : typeof callback,
|
||||
);
|
||||
viewConfigCallbacks.set(name, callback);
|
||||
return name;
|
||||
};
|
||||
@@ -94,8 +100,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
|
||||
if (typeof callback !== 'function') {
|
||||
invariant(
|
||||
false,
|
||||
'View config not found for name %s.%s',
|
||||
'View config getter callback for component `%s` must be a function (received `%s`).%s',
|
||||
name,
|
||||
callback === null ? 'null' : typeof callback,
|
||||
typeof name[0] === 'string' && /[a-z]/.test(name[0])
|
||||
? ' Make sure to start component names with a capital letter.'
|
||||
: '',
|
||||
|
||||
@@ -32,6 +32,18 @@ describe('ReactNativeError', () => {
|
||||
.computeComponentStackForErrorReporting;
|
||||
});
|
||||
|
||||
it('should throw error if null component registration getter is used', () => {
|
||||
expect(() => {
|
||||
try {
|
||||
createReactNativeComponentClass('View', null);
|
||||
} catch (e) {
|
||||
throw new Error(e.toString());
|
||||
}
|
||||
}).toThrow(
|
||||
'Invariant Violation: View config getter callback for component `View` must be a function (received `null`)',
|
||||
);
|
||||
});
|
||||
|
||||
it('should be able to extract a component stack from a native view', () => {
|
||||
const View = createReactNativeComponentClass('View', () => ({
|
||||
validAttributes: {foo: true},
|
||||
|
||||
@@ -75,6 +75,12 @@ exports.register = function(name: string, callback: ViewConfigGetter): string {
|
||||
'Tried to register two views with the same name %s',
|
||||
name,
|
||||
);
|
||||
invariant(
|
||||
typeof callback === 'function',
|
||||
'View config getter callback for component `%s` must be a function (received `%s`)',
|
||||
name,
|
||||
callback === null ? 'null' : typeof callback,
|
||||
);
|
||||
viewConfigCallbacks.set(name, callback);
|
||||
return name;
|
||||
};
|
||||
@@ -91,8 +97,9 @@ exports.get = function(name: string): ReactNativeBaseComponentViewConfig<> {
|
||||
if (typeof callback !== 'function') {
|
||||
invariant(
|
||||
false,
|
||||
'View config not found for name %s.%s',
|
||||
'View config getter callback for component `%s` must be a function (received `%s`).%s',
|
||||
name,
|
||||
callback === null ? 'null' : typeof callback,
|
||||
typeof name[0] === 'string' && /[a-z]/.test(name[0])
|
||||
? ' Make sure to start component names with a capital letter.'
|
||||
: '',
|
||||
|
||||
Reference in new issue
Block a user