cannot appear as a descendant of

前端 未结 11 507
执念已碎
执念已碎 2021-01-30 04:36

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

Warning: validateDOMnesting(...): 
cannot appear as a descendant of

. See

相关标签:
11条回答
  • 2021-01-30 05:26

    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>
        );
    }
    
    0 讨论(0)
  • 2021-01-30 05:27

    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.

    0 讨论(0)
  • 2021-01-30 05:28

    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.

    0 讨论(0)
  • 2021-01-30 05:29

    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>
    
    0 讨论(0)
  • 2021-01-30 05:34

    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.

    0 讨论(0)
提交回复
热议问题