300

I'm seeing this. It's not a mystery what it is complaining about:

Warning: validateDOMnesting(...): <div> cannot appear as a descendant of <p>. See ... SomeComponent > p > ... > SomeOtherComponent > ReactTooltip > div.

I'm the author of SomeComponent and SomeOtherComponent. But the latter is using an external dependency (ReactTooltip from react-tooltip). It's probably not essential that this is an external dependency, but it lets me try the argument here that it is "some code that's out of my control".

How worried should I be about this warning, given that the nested component is working just fine (seemingly)? And how would I go about changing this anyway (provided I don't want to re-implement an external dependency)? Is there maybe a better design that I'm yet unaware of?

For completeness sake, here's the implementation of SomeOtherComponent. It just renders this.props.value, and when hovered: a tooltip that says "Some tooltip message":

class SomeOtherComponent extends React.Component {
  constructor(props) {
    super(props)
  }

  render() {
    const {value, ...rest} = this.props;
    return <span className="some-other-component">
      <a href="#" data-tip="Some tooltip message" {...rest}>{value}</a>
      <ReactTooltip />
    </span>
  }
}

Thank you.

BiswajitPaloi
  • 586
  • 4
  • 16
Sander Verhagen
  • 8,540
  • 4
  • 41
  • 63

20 Answers20

348

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.

zayquan
  • 7,544
  • 2
  • 30
  • 39
  • 1
    It's unfortunate in terms of SEO, especially if you want the component to be a header (to be recognised), but with a variant of something like h4. – pfeds Apr 23 '19 at 05:14
  • On further faffing around it seems you can have a root Typography element with variant="inherit" (i.e. at the page level), and then a sub Typography component="h1" variant="h5". It seems to work, but I'm not entirely sure I'm using Typography the right way... – pfeds Apr 23 '19 at 05:18
  • 28
    I used `` and it works without warnings now. Thank you so much for putting me on the right track! – JohnK Aug 17 '19 at 02:52
  • I needed a typography to encapsulate my div so that my text displayed correctly on the page. Without it as a parent element my css class didn't seem to be registering correctly, strange right? However this worked like a charm, the css registered and it removed my errors, sweet! – C.Gadd Sep 01 '19 at 10:48
120

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.

Community
  • 1
  • 1
JD Hernandez
  • 1,785
  • 1
  • 20
  • 29
  • 1
    So, yes, I understood what was rendered, but would you then say that any components that render a `div` should be refactored, on the chance of being used in a `p`? It seems to fly in the face of how very popular `div`s have been for arbitrarily wrapping stuff? – Sander Verhagen Jan 30 '17 at 03:59
  • 2
    I guess so, since it is in the w3c recommendation. Also, we could easily replace a `div` with a `span` to avoid this kind of warning but without affecting anything in the logic of the code. – JD Hernandez Jan 30 '17 at 04:07
  • 1
    If you get this error when using Material UI directly you can change the "component", which I describe below in my answer. Hope this helps – zayquan Dec 10 '18 at 06:56
  • 2
    Try using instead of
    inside your

    - This should fix it. I ran into this error by using

    for displaying an image that's source is specified by a parent css class.
    – Christopher Stock Oct 16 '19 at 12:15
73

If you're looking for where this is happening, in console you can use: document.querySelectorAll(" p * div ")

Alex C
  • 1,002
  • 8
  • 5
  • 11
    Here's another version that made finding the item more verbose for me `document.querySelectorAll("p > div")` – beeeliu Sep 14 '21 at 16:00
34

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.

Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
AmerllicA
  • 29,059
  • 15
  • 130
  • 154
  • In short words Material UI has bugs? – Ero Stefano Aug 01 '20 at 10:31
  • 2
    No dear @EroStefano, this is intentional that the developer follows the rule of the hierarchy of inline and block levels. – AmerllicA Aug 01 '20 at 12:05
  • Thank you! I was trying to solve this issue past 3 days and you helped me solve it – Rohan Rao Jan 04 '23 at 15:02
  • Do you know the solution for this in Fluent UI ? Here is my post with the same problem - https://stackoverflow.com/questions/75910727/textfield-error-in-console-in-fluentui-react – coolest-duck Apr 02 '23 at 11:55
  • in my case, i got this error because of ButtonGroup which was used as secondary tag inside a ListItemText. fortunately, the warning was gone when I added the component="div" for ButtonGroup – Arifur Rahman Khan May 04 '23 at 05:45
29

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.

Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
Dion Duimel
  • 291
  • 3
  • 3
  • 8
    I used `` and it works without warnings now. Thank you so much for putting me on the right track! – JohnK Aug 17 '19 at 02:52
19

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.

Kostas Minaidis
  • 4,681
  • 3
  • 17
  • 25
jithu
  • 1,867
  • 1
  • 7
  • 16
15

The warning appears only because the demo code has:

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`simple-tabpanel-${index}`}
      aria-labelledby={`simple-tab-${index}`}
      {...other}
    >
      {value === index && (
        <Box p={3}>  // <==NOTE P TAG HERE
          <Typography>{children}</Typography>
        </Box>
      )}
    </div>
  );
}

Changing it like this takes care of it:

function TabPanel(props) {
    const {children, value, index, classes, ...other} = props;

    return (
        <div
            role="tabpanel"
            hidden={value !== index}
            id={`simple-tabpanel-${index}`}
            aria-labelledby={`simple-tab-${index}`}
            {...other}
        >
            {value === index && (
                <Container>
                    <Box>   // <== P TAG REMOVED
                        {children}
                    </Box>
                </Container>
            )}
        </div>
    );
}
VikR
  • 4,818
  • 8
  • 51
  • 96
  • 1
    curious -- that p={3} parameter of the Box component pertains to padding, right? while the error indeed has something to do with constraints of the browser -- so that a div inside a paragraph tag is considered invalid - clearly answered by @jithu. To fix this instead -- remove the component to prevent wrapping stuffs inside

    by default.

    – Vincent Edward Gedaria Binua Feb 27 '21 at 07:47
12

With Material UI !!!!!

I've spent an embarassing amount of time but thanks to this answer --> here by Alex C which gave this very simple yet smart solution

document.querySelectorAll(" p * div ")

So at my surprise , was the Material UI DialogContentText component DialogContentText API that caused the issue

So just use component={'span'} to fix the problem (it won't effect the style)

 <DialogContentText id="alert-dialog-inputs" component={'span'}/>

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
  • Do you know the solution in Fluent UI ? Here is my post with the same problem - https://stackoverflow.com/questions/75910727/textfield-error-in-console-in-fluentui-react – coolest-duck Apr 02 '23 at 11:54
  • No i never used this library. But the error u have is the same exact one. I think u have to figure it out by debugginh a bit. If i recall i found it by adding in terminal that above queryselector. Tjen found the html block and then the conponent that was causing tjis. Is not straight forward – Federico Baù Apr 02 '23 at 12:43
  • Perfect! fixed the issue I was also confused about. Material UI has some very odd 'qwirks'. Thank you :-) – James Jul 17 '23 at 16:15
  • @James you welcome. Well this is True not only for Material UI but any React lib I noticed. – Federico Baù Jul 18 '23 at 13:19
8

I got this from using a react-boostrap <Card.Text> section of a component in React. None of my components were in p tags. <Card.Text> by default shows up as a

in the HTML tree once rendered and basically having a custom React component returning its jsx elements within a <div> it causes a <p><div>...</div></p> which is a bad HTML. So to fix this issue if one is using <Card.Text> you can basically use the 'as' attribute as the following <Card.Text as='div'> and this will resolve the warning because it allow a tree such as <div><div></div></div>

DRProgrammer
  • 81
  • 1
  • 2
5

I had a similar issue and wrapped the component in "div" instead of "p" and the error went away.

jbluft
  • 51
  • 1
  • 1
4

I got this from using a custom component inside a <Card.Text> section of a <Card> component in React. None of my components were in p tags

BitShift
  • 977
  • 2
  • 9
  • 28
3

If you are using ReactTooltip, to make the warning disappear, you can now add a wrapper prop with a value of span, like this:

<ReactTooltip wrapper="span" />

Since the span is an inline element, it should no longer complain.

Nicco
  • 45
  • 1
  • 6
2

There is a problem in App.js during its rendering. Don't use <div>...</div> to render just use <>...</>.

2

I had same issue with react-bootstrap and with Alex C's solution i found that i use <div> in <Card.Text> which is kinda <p>

<Card.Text>
     <div>{name}</div>
     <div>{address}</div>
     <div>{size}</div>
</Card.Text>
Marzieh Mousavi
  • 1,213
  • 1
  • 12
  • 28
2

If you couldn't solve problem use bottom method You have this problem because, you Open Grid tag or div in text tag and It can be false. For example I write part of the code That have problem.look at it:

       <Typography>
         text
            <Grid>

            </Grid>
        </Typography>

Be careful I open Grid tag in Typography tag. So It is false if we Open Grid tag in typography tag. As far as my knowledge know when We open in typograghy tag, We are Deviating from the MUI standard. So for best way you can us this Code:

  <Grid>
       <Typography>
             text
       </Typography>
   </Grid>

If you used this code, you would not Any problem. As result if you have problem again You have to check components and See in which component you did not Choose the correct mode. You should check:

Span typography h1 h2 h3 h4 h5 h6 p

I hope I helped you .

daniel3380
  • 49
  • 3
1

I got this error when using Chakra UI in React when doing inline styling for some text. The correct way to do the inline styling was using the span element, as others also said for other styling frameworks. The correct code:

<Text as="span" fontWeight="bold">
    Bold text
    <Text display="inline" fontWeight="normal">normal inline text</Text>
</Text>
0

I resolved that issue by removing my tag that I used to wrap around my 2 textfields that were causing that same error log.

dizad87
  • 448
  • 4
  • 15
-1

i have be late on this. This error occur to me too and the reason it occur was because i was using react material TAB. in that we use something like that and there we wrap our children props inside of {children}. the reason it occur is because react dont consider as so, replace that by it work.

lodey
  • 174
  • 2
  • 8
-1

You can not descendant of

; Error: <p><div>aaaa</div></p> Solution <div><p>aaaa</p></div>

-1

(Extra) Problem can be <p> elements inside <p> element in other similar errors.

Error:

<p> 
    <p> 
    </p>
</p>
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 13 '22 at 18:13
  • 1
    Your post seems to be adding a detail of your own problem to the problem described at the top of this page. So your post does not seem to actually try to answer to the question. Please [edit] to make more obvious that you are trying to answer according to [answer]. Otherwise this will be perceived as an answer post which was misused to ask a question. – Yunnosch Oct 15 '22 at 07:34