-
Notifications
You must be signed in to change notification settings - Fork 46.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add component stack info to key validation warnings #6799
Changes from 8 commits
9d4e2ff
53d8ca3
3f112af
af4a32a
14dbe15
570fe38
5e1c16d
7274283
1a0f6ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,11 +48,25 @@ var ownerHasKeyUseWarning = {}; | |
|
||
var loggedTypeFailures = {}; | ||
|
||
function getCurrentComponentErrorInfo(parentType) { | ||
var info = getDeclarationErrorAddendum(); | ||
|
||
if (!info) { | ||
var parentName = typeof parentType === 'string' ? | ||
parentType : parentType.displayName || parentType.name; | ||
if (parentName) { | ||
info = ` Check the top-level render call using <${parentName}>.`; | ||
} | ||
} | ||
return info; | ||
} | ||
|
||
/** | ||
* Warn if the element doesn't have an explicit key assigned to it. | ||
* This element is in an array. The array could grow and shrink or be | ||
* reordered. All children that haven't already been validated are required to | ||
* have a "key" property assigned to it. | ||
* have a "key" property assigned to it. Error statuses are cached so a warning | ||
* will only be shown once. | ||
* | ||
* @internal | ||
* @param {ReactElement} element Element that requires a key. | ||
|
@@ -64,67 +78,36 @@ function validateExplicitKey(element, parentType) { | |
} | ||
element._store.validated = true; | ||
|
||
var addenda = getAddendaForKeyUse('uniqueKey', element, parentType); | ||
if (addenda === null) { | ||
// we already showed the warning | ||
return; | ||
} | ||
warning( | ||
false, | ||
'Each child in an array or iterator should have a unique "key" prop.' + | ||
'%s%s%s', | ||
addenda.parentOrOwner || '', | ||
addenda.childOwner || '', | ||
addenda.url || '' | ||
var memoizer = ownerHasKeyUseWarning.uniqueKey || ( | ||
ownerHasKeyUseWarning.uniqueKey = {} | ||
); | ||
} | ||
|
||
/** | ||
* Shared warning and monitoring code for the key warnings. | ||
* | ||
* @internal | ||
* @param {string} messageType A key used for de-duping warnings. | ||
* @param {ReactElement} element Component that requires a key. | ||
* @param {*} parentType element's parent's type. | ||
* @returns {?object} A set of addenda to use in the warning message, or null | ||
* if the warning has already been shown before (and shouldn't be shown again). | ||
*/ | ||
function getAddendaForKeyUse(messageType, element, parentType) { | ||
var addendum = getDeclarationErrorAddendum(); | ||
if (!addendum) { | ||
var parentName = typeof parentType === 'string' ? | ||
parentType : parentType.displayName || parentType.name; | ||
if (parentName) { | ||
addendum = ` Check the top-level render call using <${parentName}>.`; | ||
} | ||
} | ||
|
||
var memoizer = ownerHasKeyUseWarning[messageType] || ( | ||
ownerHasKeyUseWarning[messageType] = {} | ||
); | ||
if (memoizer[addendum]) { | ||
return null; | ||
var currentComponentErrorInfo = getCurrentComponentErrorInfo(parentType); | ||
if (memoizer[currentComponentErrorInfo]) { | ||
return; | ||
} | ||
memoizer[addendum] = true; | ||
|
||
var addenda = { | ||
parentOrOwner: addendum, | ||
url: ' See https://fb.me/react-warning-keys for more information.', | ||
childOwner: null, | ||
}; | ||
memoizer[currentComponentErrorInfo] = true; | ||
|
||
// Usually the current owner is the offender, but if it accepts children as a | ||
// property, it may be the creator of the child that's responsible for | ||
// assigning it a key. | ||
var childOwner = ''; | ||
if (element && | ||
element._owner && | ||
element._owner !== ReactCurrentOwner.current) { | ||
// Give the component that originally created this child. | ||
addenda.childOwner = | ||
childOwner = | ||
` It was passed a child from ${element._owner.getName()}.`; | ||
} | ||
|
||
return addenda; | ||
warning( | ||
false, | ||
'Each child in an array or iterator should have a unique "key" prop.' + | ||
'%s%s See https://fb.me/react-warning-keys for more information.%s', | ||
currentComponentErrorInfo, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed the error info object packing and unpacking logic and moved the |
||
childOwner, | ||
ReactComponentTreeDevtool.getCurrentStackAddendum(element) | ||
); | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,10 @@ var ReactDOM; | |
var ReactTestUtils; | ||
|
||
describe('ReactElementValidator', function() { | ||
function normalizeCodeLocInfo(str) { | ||
return str.replace(/\(at .+?:\d+\)/g, '(at **)'); | ||
} | ||
|
||
var ComponentClass; | ||
|
||
beforeEach(function() { | ||
|
@@ -95,9 +99,10 @@ describe('ReactElementValidator', function() { | |
ReactTestUtils.renderIntoDocument(<Anonymous>{divs}</Anonymous>); | ||
|
||
expect(console.error.argsForCall.length).toBe(1); | ||
expect(console.error.argsForCall[0][0]).toBe( | ||
expect(normalizeCodeLocInfo(console.error.argsForCall[0][0])).toBe( | ||
'Warning: Each child in an array or iterator should have a unique ' + | ||
'"key" prop. See https://fb.me/react-warning-keys for more information.' | ||
'"key" prop. See https://fb.me/react-warning-keys for more information.\n' + | ||
' in div (at **)' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if this had both the parent component (Anonymous, or "Unknown" as we'll know it as in the devtools) and the child (div) here. Is that possible? I guess we would probably have to make getCurrentStackAddendum take both elements as arguments. |
||
); | ||
}); | ||
|
||
|
@@ -111,10 +116,46 @@ describe('ReactElementValidator', function() { | |
ReactTestUtils.renderIntoDocument(<div>{divs}</div>); | ||
|
||
expect(console.error.argsForCall.length).toBe(1); | ||
expect(console.error.argsForCall[0][0]).toBe( | ||
expect(normalizeCodeLocInfo(console.error.argsForCall[0][0])).toBe( | ||
'Warning: Each child in an array or iterator should have a unique ' + | ||
'"key" prop. Check the top-level render call using <div>. See ' + | ||
'https://fb.me/react-warning-keys for more information.' | ||
'https://fb.me/react-warning-keys for more information.\n' + | ||
' in div (at **)' | ||
); | ||
}); | ||
|
||
it('warns for keys with component stack info', function() { | ||
spyOn(console, 'error'); | ||
|
||
var Component = React.createClass({ | ||
render: function() { | ||
return <div>{[<div />, <div />]}</div>; | ||
}, | ||
}); | ||
|
||
var Parent = React.createClass({ | ||
render: function() { | ||
return React.cloneElement(this.props.child); | ||
}, | ||
}); | ||
|
||
var GrandParent = React.createClass({ | ||
render: function() { | ||
return <Parent child={<Component />} />; | ||
}, | ||
}); | ||
|
||
ReactTestUtils.renderIntoDocument(<GrandParent />); | ||
|
||
expect(console.error.argsForCall.length).toBe(1); | ||
expect(normalizeCodeLocInfo(console.error.argsForCall[0][0])).toBe( | ||
'Warning: Each child in an array or iterator should have a unique ' + | ||
'"key" prop. Check the render method of `Component`. See ' + | ||
'https://fb.me/react-warning-keys for more information.\n' + | ||
' in div (at **)\n' + | ||
' in Component (at **)\n' + | ||
' in Parent (at **)\n' + | ||
' in GrandParent (at **)' | ||
); | ||
}); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,28 @@ function purgeDeep(id) { | |
} | ||
} | ||
|
||
function describeComponentFrame(name, source, ownerName) { | ||
return '\n in ' + name + ( | ||
source ? | ||
' (at ' + source.fileName.replace(/^.*[\\\/]/, '') + ':' + | ||
source.lineNumber + ')' : | ||
ownerName ? | ||
' (created by ' + ownerName + ')' : | ||
'' | ||
); | ||
} | ||
|
||
function describeID(id) { | ||
var name = ReactComponentTreeDevtool.getDisplayName(id); | ||
var element = ReactComponentTreeDevtool.getElement(id); | ||
var ownerID = ReactComponentTreeDevtool.getOwnerID(id); | ||
var ownerName; | ||
if (ownerID) { | ||
ownerName = ReactComponentTreeDevtool.getDisplayName(ownerID); | ||
} | ||
return describeComponentFrame(name, element._source, ownerName); | ||
} | ||
|
||
var ReactComponentTreeDevtool = { | ||
onSetDisplayName(id, displayName) { | ||
updateTree(id, item => item.displayName = displayName); | ||
|
@@ -154,28 +176,6 @@ var ReactComponentTreeDevtool = { | |
}, | ||
|
||
getCurrentStackAddendum(topElement) { | ||
function describeComponentFrame(name, source, ownerName) { | ||
return '\n in ' + name + ( | ||
source ? | ||
' (at ' + source.fileName.replace(/^.*[\\\/]/, '') + ':' + | ||
source.lineNumber + ')' : | ||
ownerName ? | ||
' (created by ' + ownerName + ')' : | ||
'' | ||
); | ||
} | ||
|
||
function describeID(id) { | ||
var name = ReactComponentTreeDevtool.getDisplayName(id); | ||
var element = ReactComponentTreeDevtool.getElement(id); | ||
var ownerID = ReactComponentTreeDevtool.getOwnerID(id); | ||
var ownerName; | ||
if (ownerID) { | ||
ownerName = ReactComponentTreeDevtool.getDisplayName(ownerID); | ||
} | ||
return describeComponentFrame(name, element._source, ownerName); | ||
} | ||
|
||
var info = ''; | ||
if (topElement) { | ||
var type = topElement.type; | ||
|
@@ -192,11 +192,17 @@ var ReactComponentTreeDevtool = { | |
|
||
var currentOwner = ReactCurrentOwner.current; | ||
var id = currentOwner && currentOwner._debugID; | ||
|
||
info += ReactComponentTreeDevtool.getStackAddendumByID(id); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
return info; | ||
}, | ||
|
||
getStackAddendumByID(id) { | ||
var info = ''; | ||
while (id) { | ||
info += describeID(id); | ||
id = ReactComponentTreeDevtool.getParentID(id); | ||
} | ||
|
||
return info; | ||
}, | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,22 +13,24 @@ | |
|
||
var ReactReconciler = require('ReactReconciler'); | ||
|
||
var ReactComponentTreeDevtool = require('ReactComponentTreeDevtool'); | ||
var instantiateReactComponent = require('instantiateReactComponent'); | ||
var KeyEscapeUtils = require('KeyEscapeUtils'); | ||
var shouldUpdateReactComponent = require('shouldUpdateReactComponent'); | ||
var traverseAllChildren = require('traverseAllChildren'); | ||
var warning = require('warning'); | ||
|
||
function instantiateChild(childInstances, child, name) { | ||
function instantiateChild(childInstances, child, name, selfDebugID) { | ||
// We found a component instance. | ||
var keyUnique = (childInstances[name] === undefined); | ||
if (__DEV__) { | ||
warning( | ||
keyUnique, | ||
'flattenChildren(...): Encountered two children with the same key, ' + | ||
'`%s`. Child keys must be unique; when two children share a key, only ' + | ||
'the first child will be used.', | ||
KeyEscapeUtils.unescape(name) | ||
'the first child will be used.%s', | ||
KeyEscapeUtils.unescape(name), | ||
ReactComponentTreeDevtool.getStackAddendumByID(selfDebugID) | ||
); | ||
} | ||
if (child != null && keyUnique) { | ||
|
@@ -50,12 +52,31 @@ var ReactChildReconciler = { | |
* @return {?object} A set of child instances. | ||
* @internal | ||
*/ | ||
instantiateChildren: function(nestedChildNodes, transaction, context) { | ||
instantiateChildren: function( | ||
nestedChildNodes, | ||
transaction, | ||
context, | ||
selfDebugID // __DEV__ only | ||
) { | ||
if (nestedChildNodes == null) { | ||
return null; | ||
} | ||
var childInstances = {}; | ||
traverseAllChildren(nestedChildNodes, instantiateChild, childInstances); | ||
|
||
if (__DEV__) { | ||
traverseAllChildren( | ||
nestedChildNodes, | ||
(childInsts, child, name) => instantiateChild( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, this seems fine to me. (I think calling it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I thought about this; |
||
childInsts, | ||
child, | ||
name, | ||
selfDebugID | ||
), | ||
childInstances | ||
); | ||
} else { | ||
traverseAllChildren(nestedChildNodes, instantiateChild, childInstances); | ||
} | ||
return childInstances; | ||
}, | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@spicyj what's a good way to get and feed
debugID
toflattenChildren
here? The only place that callsReactTransitionChildMapping.getChildMapping
(syntactic sugar forflattenChildren
) is here: https://github.com/facebook/react/blob/master/src/addons/transitions/ReactTransitionGroup.js#L41. In other words, how do I/can I get a debugID from a component?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, seems like the only other place we use
flattenChildren
is inReactMultiChild
and there's already a test for that inReactMultiChild-test
. Interestingly, whenReactChildReconciler
finds duplicated keys, the error message talks about "flattenChildren" (which is also test covered in the newReactChildReconciler-test
file).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should work. Maybe someday we'll have a public API to get this that anyone could use but for now I think it's okay for us to get the internal instance for this here.