javascript - 警告:validateDOMNesting(...):不能作为使用 React-bootstrap 的孩子出现
问题描述
我有这个代码。这和更多在方法的return
声明中render()
<Table striped bordered condensed hover responsive>
<thead>
<tr>
<th style={{textAlign: 'center'}}>Imie Nazwisko/Nazwa firmy</th>
<th style={{textAlign: 'center'}}>ID</th>
<th style={{textAlign: 'center'}}>Raporty</th>
</tr>
</thead>
<tbody>
{
this.state.users && this.state.users.map((user, i) => (
<tr key={user.id}>
<td>
{user.userType === 'person' ? `${user.person.firstName} ${user.person.lastName}` : user.company.name}
</td>
<td>{user.id}</td>
<Table striped bordered hover responsive>
<tbody>
{
user.reports.map(report => (
<tr key={report.id}>
<td>{report.id}</td>
</tr>
))
}
</tbody>
</Table>
</tr>
))
}
</tbody>
</Table>
而这个错误代码:
Warning: validateDOMNesting(...): <div> cannot appear as a child of <tr>.
in div (created by Table)
in Table (at ReportsComponent.js:59)
in tr (at ReportsComponent.js:54)
in tbody (at ReportsComponent.js:51)
in table (created by Table)
in div (created by Table)
in Table (at ReportsComponent.js:43)
in ReportsComponent (at NavigationComponent.js:67)
所以问题是我自己没有创建 div。我可以看到 Table 正在创建引发错误的 div。
我正在使用react-bootstrap中的Table组件。
这段代码在浏览器中看起来不错,并且似乎可以工作,但错误只是令人恼火。我该如何解决?
解决方案
div
React Bootstrap在添加响应属性时会生成一个表格。为避免此警告(注意,不是阻塞错误),您可以将Table
组件包装在td
.
或者,如果您愿意,您可以在 React Bootstrap 的 Table 周围编写一个包装器,以确保在td
传入 responsive 属性时它被包围。
推荐阅读
- java - Twitter 仅应用程序身份验证 - 错误代码 86“此方法需要 GET 或 HEAD”
- java - Java:打印出数组中的每个数字而不打印该数字的重复?
- ios - 动画后屏幕上残留的iOS模拟器鬼影边缘
- angular - Angular 5 [ngForOf] 仅显示循环中的最后一条记录
- java - Equals 语句对同一个字母的大写和小写都有效
- php - 尝试使用 PHP 在所有网站页面中包含相同的标题(mac)
- sql-server - 如何将 TimeSpan 添加到 SQL Server 中的时间列
- android-studio - 两个视图,无法选择 ListView
- c# - 在 C# 中验证小费计算器中的数据
- identityserver4 - 生产中 https 上的身份服务器 4 错误