When a user puts a textnode (whitespace) in an illegal location, our warning complains about a span element (because we wrap text - including whitespace - in a span). This is confusing for new users, since there is no span present in their code and the introduction of the span tag is an implementation detail of React. We should fix the warning to better align with the code the user is looking at.
var rows = <tr></tr>;
React.render(<table><tbody> {rows} </tbody></table>, document.getElementById('container'));
Most helpful comment
It took me nearly one hour until I came to this page to know about the actual cause of warning.
There were one whitespace between my
Really hard to resolve by this message.
All 20 comments
See https://github.com/facebook/react/pull/5098Need to cover all cases...
<span></span> in <table></table><span></span> in <tbody></tbody><span></span> in <thead></thead><span></span> in <tfoot></tfoot><span></span> in <tr></tr>any others I am missing? See revised https://github.com/facebook/react/pull/5099
@spicyj I think this might be a bit advanced to be a good-first-bug. Specifically, we need to handle three general cases:
<table><span>foo</span></table>. In which case, we should indicate that there is a span.<table>foo</table><table> </table>. In which case, we should call out the whitespace, because if we just call it text, it'll be easy to miss and be confused about what's going on. Whitespace includes tabs, newlines, and spaces.@jimfb Hmmm, looks like something is wrong with master branch (from your above comments and example). The code snippet
with master prints the warning:
But it says
undefinednotspan. However with 0.14 branch, I can see the warning withspan. I was about to fix this issue, but got confused withundefinedthing.Apparently the code on validation part from 0.14 and master are different though:
(0.14 branch):
(master)
In this case
this._tagisundefinedas we didn't wrap our text component in any tag. But doing so with say for exampleptag, printspinstead ofundefined@jimfb The mentioned three cases by you make sense. I had an rough idea for fixing this issue:
validateDOMNestingactually does validation onReactDOMTextComponent. I thought I can check viaObject.prototype.toString.callthe type of incoming object tovalidateDOMNesting. Suggest this is the right way in the react community.ReactDOMTextComponent, then we can redirect the check to say other method, where we can check these cases and appropriately print the warnings as desired.Let me know if my thought process is correct or not.
I could take a guess on these, but I think it would be best if @spicyj could jump in here, as he wrote the validation code and knows it best.
Thanks, that's a bug, fixed in #5198.
@spicyj Looks like you have just fixed the
undefinedpart. Still this issue remains right?Still the warning includes
spanin its text.@spicyj Ping.
Yes.
@spicyj: I thought of doing the fix for this issue, so on this validations line :
https://github.com/facebook/react/blob/master/src/renderers/dom/shared/ReactDOMTextComponent.js#L96
I thought of changing this to roughly :
I tested it manually in the browser and it works as expected. But the message is getting displayed as (even for text it adds
<and>):if we do:
Kindly let me know if the approach taken over here is correct. So that I can go and give a PR.
@spicyj Ping.
You can change the signature of validateDOMNesting if you have to. Also, your check for whitespace isn't right: it should only refer to whitespace if all of the text is whitespace, not if there's any whitespace character (which is what you have).
Looks like this was fixed in #5753 ? Was thinking of picking it up, but upon investigation, it appears fixed and maybe this can be closed.
We don't explicitly call out whitespace but yeah, hopefully this is clear enough now.
I'm getting whitespace in here somehow:
Rendered output is:
Where is
react-text: 315coming from? JSX should strip any non-explicit whitespace, shouldn't it?Edit: Nvm. Sorry guys.
data.billing_typeis an empty string, causingdata.billing_type && <tr>...</tr>to evaluate to an empty string. I suppose it's good that warning kicked in and told me something funky was up, so thanks for that ;)Slapping
!!before it coerce it into a bool works, but it gets pretty nasty writing conditionals in JSX this way.@mnpenner I explicitly only use
if ? then : elsefor exactly that reason, it's just less error prone overall.I think we can actually relax the warning now since we don't have spans. Certainly for an empty string.
It took me nearly one hour until I came to this page to know about the actual cause of warning.
There were one whitespace between my
Really hard to resolve by this message.
@Ajaybhardwaj7 I had the same issue. Thanks for confirming that I wasn't crazy.
React should really consider changing the Error message of
#text cannot appear as a child of...to a warning about JSX spacing. Would save people much time.Okay, #7515 should clarify.
Related issues