Deduplicate logic in ReactElementValidator
Shouldn't be much change. Notably, this calls `.getName()` instead of trying to derive it manually, which is more consistent.
Ben Alpert committed
Aug 7, 2015 at 18:53 UTC
3ec9f8657308ebba7ce5f76206ce7eeaec9aa8bd
3 files changed
+14
-44
src/isomorphic/classic/element/ReactElementValidator.js
+12
-42
@@ -49,37 +49,6 @@ var loggedTypeFailures = {};
49
50
var NUMERIC_PROPERTY_REGEX = /^\d+$/;
51
52
-/**
53
- * Gets the instance's name for use in warnings.
54
- *
55
- * @internal
56
- * @return {?string} Display name or undefined
57
- */
58
-function getName(instance) {
59
- var publicInstance = instance && instance.getPublicInstance();
60
- if (!publicInstance) {
61
- return undefined;
62
- }
63
- var constructor = publicInstance.constructor;
64
- if (!constructor) {
65
- return undefined;
66
- }
67
- return constructor.displayName || constructor.name || undefined;
68
-}
69
-
70
-/**
71
- * Gets the current owner's displayName for use in warnings.
72
- *
73
- * @internal
74
- * @return {?string} Display name or undefined
75
- */
76
-function getCurrentOwnerDisplayName() {
77
- var current = ReactCurrentOwner.current;
78
- return (
79
- current && getName(current) || undefined
80
- );
81
-}
82
-
52
/**
53
* Warn if the element doesn't have an explicit key assigned to it.
54
* This element is in an array. The array could grow and shrink or be
@@ -150,24 +119,25 @@ function validatePropertyKey(name, element, parentType) {
119
* if the warning has already been shown before (and shouldn't be shown again).
120
*/
121
function getAddendaForKeyUse(messageType, element, parentType) {
153
- var ownerName = getCurrentOwnerDisplayName();
154
- var parentName = typeof parentType === 'string' ?
155
- parentType : parentType.displayName || parentType.name;
122
+ var addendum = getDeclarationErrorAddendum();
123
+ if (!addendum) {
124
+ var parentName = typeof parentType === 'string' ?
125
+ parentType : parentType.displayName || parentType.name;
126
+ if (parentName) {
127
+ addendum = ` Check the React.render call using <${parentName}>.`;
128
+ }
129
+ }
130
157
- var useName = ownerName || parentName;
131
var memoizer = ownerHasKeyUseWarning[messageType] || (
132
ownerHasKeyUseWarning[messageType] = {}
133
);
161
- if (memoizer[useName]) {
134
+ if (memoizer[addendum]) {
135
return null;
136
}
164
- memoizer[useName] = true;
137
+ memoizer[addendum] = true;
138
139
var addenda = {
167
- parentOrOwner:
168
- ownerName ? ` Check the render method of ${ownerName}.` :
169
- parentName ? ` Check the React.render call using <${parentName}>.` :
170
- null,
140
+ parentOrOwner: addendum,
141
url: ' See https://fb.me/react-warning-keys for more information.',
142
childOwner: null,
143
};
@@ -180,7 +150,7 @@ function getAddendaForKeyUse(messageType, element, parentType) {
150
element._owner !== ReactCurrentOwner.current) {
151
// Give the component that originally created this child.
152
addenda.childOwner =
183
- ` It was passed a child from ${getName(element._owner)}.`;
153
+ ` It was passed a child from ${element._owner.getName()}.`;
154
}
155
156
return addenda;
src/isomorphic/classic/element/__tests__/ReactElementValidator-test.js
+1
-1
@@ -77,7 +77,7 @@ describe('ReactElementValidator', function() {
77
expect(console.error.argsForCall.length).toBe(1);
78
expect(console.error.argsForCall[0][0]).toContain(
79
'Each child in an array or iterator should have a unique "key" prop. ' +
80
- 'Check the render method of InnerClass. ' +
80
+ 'Check the render method of `InnerClass`. ' +
81
'It was passed a child from ComponentWrapper. '
82
);
83
});
src/isomorphic/modern/element/__tests__/ReactJSXElementValidator-test.js
+1
-1
@@ -83,7 +83,7 @@ describe('ReactJSXElementValidator', function() {
83
expect(console.error.argsForCall.length).toBe(1);
84
expect(console.error.argsForCall[0][0]).toContain(
85
'Each child in an array or iterator should have a unique "key" prop. ' +
86
- 'Check the render method of InnerComponent. ' +
86
+ 'Check the render method of `InnerComponent`. ' +
87
'It was passed a child from ComponentWrapper. '
88
);
89
});