If this error occurs while using Material UI <Typography>
https://material-ui.com/api/typography/, then you can easily change the <p>
to a <span>
by changing the value of the component
attribute of the <Typography>
element :
<Typography component={'span'} variant={'body2'}>
According to the typography docs:
component : The component used for the root node. Either a string to use a DOM element or a component. By default, it maps the variant to a good default headline component.
So Typography is picking <p>
as a sensible default, which you can change. May come with side effects ... worked for me.
Based on the warning message, the component ReactTooltip renders an HTML that might look like this:
<p>
<div>...</div>
</p>
According to this document, a <p></p>
tag can only contain inline elements. That means putting a <div></div>
tag inside it should be improper, since the div
tag is a block element. Improper nesting might cause glitches like rendering extra tags, which can affect your javascript and css.
If you want to get rid of this warning, you might want to customize the ReactTooltip component, or wait for the creator to fix this warning.
If you're looking for where this is happening, in console you can use: document.querySelectorAll(" p * div ")
Your component might be rendered inside another component (such as a <Typography> ... </Typography>
). Therefore, it will load your component inside a <p> .. </p>
which is not allowed.
Fix:
Remove <Typography>...</Typography>
because this is only used for plain text inside a <p>...</p>
or any other text element such as headings.
I got this warning by using Material UI components, then I test the component="div"
as prop to the below code and everything became correct:
import Grid from '@material-ui/core/Grid';
import Typography from '@material-ui/core/Typography';
<Typography component="span">
<Grid component="span">
Lorem Ipsum
</Grid>
</Typography>
Actually, this warning happens because in the Material UI the default HTML tag of Grid
component is div
tag and the default Typography
HTML tag is p
tag, So now the warning happens,
Warning: validateDOMnesting(...): <div> cannot appear as a descendant of <p>
Details (and some HTML theory regarding the warning) : The <div> cannot appear as a descendant of <p>
message is shown due to the fact that the permitted content
of a <p>
tag is according to the standards set to the Phrasing Context
which does not include <div>
tags. See the links for more details.
This is a constraint of browsers. You should use div or article or something like that in the render method of App because that way you can put whatever you like inside it. Paragraph tags are limited to only containing a limited set of tags (mostly tags for formatting text. You cannot have a div inside a paragraph
<p><div></div></p>
is not valid HTML. Per the tag omission rules listed in the spec, the <p>
tag is automatically closed by the <div>
tag, which leaves the </p>
tag without a matching <p>
. The browser is well within its rights to attempt to correct it by adding an open <p>
tag after the <div>
:
<p></p><div></div><p></p>
You can't put a <div>
inside a <p>
and get consistent results from various browsers. Provide the browsers with valid HTML and they will behave better.
You can put <div>
inside a <div>
though so if you replace your <p>
with <div class="p">
and style it appropriately, you can get what you want.
Details (and some HTML theory regarding the warning) : The <div> cannot appear as a descendant of <p>
message is shown due to the fact that the permitted content
of a <p>
tag is according to the standards set to the Phrasing Context
which does not include <div>
tags. See the links for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With