我看到了这个。它在抱怨什么并不神秘:

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

我是SomeComponent和SomeOtherComponent的作者。但后者使用了外部依赖项(来自react-tooltip的ReactTooltip)。这可能并不一定是一个外部依赖,但它让我尝试在这里讨论它是“一些超出我控制的代码”。

鉴于嵌套组件工作正常(看起来),我应该对这个警告有多担心呢?无论如何,我该如何改变它(如果我不想重新实现一个外部依赖)?有没有我还不知道的更好的设计?

为了完整起见,下面是SomeOtherComponent的实现。它只是渲染this。props。值,当鼠标悬停时:显示“一些工具提示消息”:

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>
  }
}

谢谢你!


当前回答

如果你正在寻找发生这种情况的地方,在控制台中你可以使用:document。querySelectorAll(" p * div ")

其他回答

如果在使用Material UI <Typography> https://material-ui.com/api/typography/时发生此错误,那么您可以通过更改<Typography>元素的组件属性值轻松地将<p>更改为<span>:

<Typography component={'span'} variant={'body2'}>

根据排版文档:

component:用于根节点的组件。使用DOM元素或组件的字符串。默认情况下,它将变体映射到一个良好的默认标题组件。

因此Typography选择<p>作为合理的默认值,这是可以更改的。可能会有副作用…为我工作。

我从<Card内使用一个自定义组件得到了这个。在React中<Card>组件的文本>部分。我的组件都不在p标签中

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>

材质UI !!!!!

我花了大量的时间,但感谢Alex C的回答——>,它给出了一个非常简单而聪明的解决方案

document.querySelectorAll(" p * div ")

所以让我惊讶的是,导致这个问题的是Material UI DialogContentText组件DialogContentText API

所以只需使用component={'span'}来解决这个问题(它不会影响样式)

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

你不能后人; 错误:< p > < div > aaaa < / div > < / p > 解决方案< div > < p > aaaa < / p > < / div >