-
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
Improve validateDOMNesting message for whitespace #7515
Changes from all commits
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 |
---|---|---|
|
@@ -254,9 +254,9 @@ function optionPostMount() { | |
ReactDOMOption.postMountWrapper(inst); | ||
} | ||
|
||
var setContentChildForInstrumentation = emptyFunction; | ||
var setAndValidateContentChildDev = emptyFunction; | ||
if (__DEV__) { | ||
setContentChildForInstrumentation = function(content) { | ||
setAndValidateContentChildDev = function(content) { | ||
var hasExistingContent = this._contentDebugID != null; | ||
var debugID = this._debugID; | ||
// This ID represents the inlined child that has no backing instance: | ||
|
@@ -270,6 +270,7 @@ if (__DEV__) { | |
return; | ||
} | ||
|
||
validateDOMNesting(null, String(content), this, this._ancestorInfo); | ||
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. Is this a good place to put it? Since the function is called 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'll rename it. |
||
this._contentDebugID = contentDebugID; | ||
if (hasExistingContent) { | ||
ReactInstrumentation.debugTool.onBeforeUpdateComponent(contentDebugID, content); | ||
|
@@ -497,7 +498,7 @@ function ReactDOMComponent(element) { | |
this._flags = 0; | ||
if (__DEV__) { | ||
this._ancestorInfo = null; | ||
setContentChildForInstrumentation.call(this, null); | ||
setAndValidateContentChildDev.call(this, null); | ||
} | ||
} | ||
|
||
|
@@ -605,7 +606,7 @@ ReactDOMComponent.Mixin = { | |
if (parentInfo) { | ||
// parentInfo should always be present except for the top-level | ||
// component when server rendering | ||
validateDOMNesting(this._tag, this, parentInfo); | ||
validateDOMNesting(this._tag, null, this, parentInfo); | ||
} | ||
this._ancestorInfo = | ||
validateDOMNesting.updatedAncestorInfo(parentInfo, this._tag, this); | ||
|
@@ -801,7 +802,7 @@ ReactDOMComponent.Mixin = { | |
// TODO: Validate that text is allowed as a child of this node | ||
ret = escapeTextContentForBrowser(contentToUse); | ||
if (__DEV__) { | ||
setContentChildForInstrumentation.call(this, contentToUse); | ||
setAndValidateContentChildDev.call(this, contentToUse); | ||
} | ||
} else if (childrenToUse != null) { | ||
var mountImages = this.mountChildren( | ||
|
@@ -843,7 +844,7 @@ ReactDOMComponent.Mixin = { | |
if (contentToUse != null) { | ||
// TODO: Validate that text is allowed as a child of this node | ||
if (__DEV__) { | ||
setContentChildForInstrumentation.call(this, contentToUse); | ||
setAndValidateContentChildDev.call(this, contentToUse); | ||
} | ||
DOMLazyTree.queueText(lazyTree, contentToUse); | ||
} else if (childrenToUse != null) { | ||
|
@@ -1117,7 +1118,7 @@ ReactDOMComponent.Mixin = { | |
if (lastContent !== nextContent) { | ||
this.updateTextContent('' + nextContent); | ||
if (__DEV__) { | ||
setContentChildForInstrumentation.call(this, nextContent); | ||
setAndValidateContentChildDev.call(this, nextContent); | ||
} | ||
} | ||
} else if (nextHtml != null) { | ||
|
@@ -1129,7 +1130,7 @@ ReactDOMComponent.Mixin = { | |
} | ||
} else if (nextChildren != null) { | ||
if (__DEV__) { | ||
setContentChildForInstrumentation.call(this, null); | ||
setAndValidateContentChildDev.call(this, null); | ||
} | ||
|
||
this.updateChildren(nextChildren, transaction, context); | ||
|
@@ -1196,7 +1197,7 @@ ReactDOMComponent.Mixin = { | |
this._wrapperState = null; | ||
|
||
if (__DEV__) { | ||
setContentChildForInstrumentation.call(this, null); | ||
setAndValidateContentChildDev.call(this, null); | ||
} | ||
}, | ||
|
||
|
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.
Could this message be confusing? Whitespace doesn't matter between JSX tags -- doesn't this problem only occur when there's whitespace inside a variable?
Edit: Nevermind, per this blog post, whitespace can appear when:
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.
Whitespace between tags on a single line matters, and I believe that's the most common cause of extra whitespace. See "Foo" in the test case below.
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.
Aha, didn't know that. I guess that makes sense, but it's not something you normally think about! (Which is why this warning is helpful!)