[英]How to fix Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>
im passing users list as a props to UserItem Component to make iterate on user list and displaying them on table.我将用户列表作为道具传递给 UserItem 组件,以迭代用户列表并将它们显示在表上。 the list is displayed correctly and i dont have any divs in my render return but i still get the error: index.js:1446 Warning: validateDOMNesting(...): cannot appear as a child of.列表显示正确,我的渲染返回中没有任何 div,但我仍然收到错误:index.js:1446 Warning: validateDOMNesting(...): cannot appear as a child of.
tried many solutions found online but none of them worked尝试了网上找到的许多解决方案,但没有一个有效
UsersManagement code:用户管理代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Spinner from './common/Spinner';
import { getUsers } from '../actions/userActions';
import UserItem from './UserItem';
class UsersManagement extends Component {
componentDidMount() {
if (!this.props.auth.isAuthenticated) {
this.props.history.push('/login');
}
this.props.getUsers();
}
render() {
const { users, loading } = this.props.user;
let usersList;
if (users === null || loading) {
usersList = <Spinner />
} else {
if (users.length > 0) {
usersList = users.map(user => (
<UserItem key={user._id} user={user} />
))
} else {
usersList = <h2>No users</h2>
}
}
return (
<div className="row">
<div className="col-12">
<h1 className="text-center mb-2">Users Management</h1>
<button type="button" className="btn btn-success mb-4">New User</button>
<table className="table">
<thead>
<tr>
<th scope="col">Options</th>
<th scope="col">Username</th>
<th scope="col">Email</th>
<th scope="col">Phone Number</th>
</tr>
</thead>
<tbody>
{usersList}
</tbody>
</table>
</div>
</div>
)
}
}
UsersManagement.propTypes = {
getUsers: PropTypes.func.isRequired,
auth: PropTypes.object.isRequired,
user: PropTypes.object.isRequired
}
const mapStateToProps = state => ({
auth: state.auth,
user: state.user
})
export default connect(mapStateToProps, {
getUsers
})(UsersManagement);
UserItem code:用户项目代码:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
class UserItem extends Component {
render() {
const { user } = this.props;
console.log(user);
return (
<tr>
<th scope="row">
<button type="button" className="btn btn-primary fa-xs mr-1"><i className="fas fa-pencil-alt"></i></button>
<button type="button" className="btn btn-danger fa-xs"><i className="far fa-trash-alt"></i></button>
</th>
<td>{user.username}</td>
<td>{user.email}</td>
<td>{user.phone}</td>
</tr>
)
}
}
UserItem.propTypes = {
user: PropTypes.object.isRequired
}
export default UserItem;
i expect to to fix the warning message我希望修复警告信息
Most likely the component Spinner
renders a <div>
as the outermost node.组件Spinner
最有可能将<div>
呈现为最外层节点。 Check the implementation of it.检查它的执行情况。
You implicitly render it inside <tbody>
through the lines您通过线条在<tbody>
内隐式呈现它
<tbody>
{usersList}
</tbody>
where usersList
defaults to <Spinner />
when there are no users or loading
is true
.当没有用户或loading
为true
时, usersList
默认为<Spinner />
。 This is why get the error.这就是为什么会出现错误。
A fix would be to wrap the Spinner
into a td
that spans the whole row:一个修复方法是将Spinner
包装到一个跨越整行的td
中:
if (users === null || loading) {
usersList = <tr><td colSpan="4"><Spinner /></td></tr>;
} else {
// ...
}
I had the same problem.我有同样的问题。 I had a external spinner component that I needed to inject.我有一个需要注入的外部微调器组件。 This is how i solved it:这就是我解决它的方法:
<table className="table profit-withdrawal">
<thead className="thead-default">
<tr>
<th>Nick Name</th>
<th>Transfer to</th>
<th>Details</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<DotsLoaderComponent variant="dark" dimension="large" />
</td>
</tr>
{this.renderTableData()}
</tbody>
</table>
So I wrapped my spinner component inside a:所以我将我的微调器组件包裹在一个:
<tr>
<td>YOUR COMPONENT HERE</td>
</tr>
validateDOMNesting(...): cannot appear as a child of <div>
. validateDOMNesting(...): 不能作为<div>
的子项出现。 I see an error similar to this You replace : tag <body> to <div>
我看到类似于此的错误您替换:标记<body> to <div>
It may not be your case, but it may be someone else's.这可能不是你的情况,但它可能是别人的。 It's kind of embarrassing but I had this problem because I was importing the wrong component:这有点尴尬,但我遇到了这个问题,因为我导入了错误的组件:
import Table from 'react-bootstrap/Col'; <-- Is a Col not a Table
Check if the imports are correct检查导入是否正确
In case someone gets this error and use render like :如果有人收到此错误并使用如下渲染:
...
{usersData ? (
Object.values(usersData)?.map((user: any) => (
<div key={user?.id}>
<tr>
<td>{user?.id}1</td>
<td>2</td>
<td>3</td>
</tr>
</div>
))
) : (
<Box>
{' '}
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
</Box>
)}
...
two mistakes here:这里有两个错误:
Box is a div, and tbody needs only tr, td, so remove div or Box from render to be like: box是一个div,tbody只需要tr,td,所以从render中去掉div或者Box就变成了这样:
<table className="table-admin">
<thead>
<tr>
<th className="th-a">ID</th>
<th className="th-a">EMAIL</th>
<th className="th-a">NAME</th>
<th className="th-a"></th>
</tr>
</thead>
<tbody>
{usersData ? (
Object.values(usersData)?.map((user: any) => (
<tr key={user?.id}>
<td>{user?.id}</td>
<td>{user?.email}</td>
<td>{user?.name}</td>
</tr>
))
) : (
<tr>
<td>N/A</td>
</tr>
)}
</tbody>
</table>
Alternative with React Portal.用 React Portal 替代。 You could use React Portals to pass the component to another dom node.您可以使用 React Portals 将组件传递给另一个 dom 节点。 This technique works well with toasts, modals, spinners etc. kind of elements that can be used as individual dom nodes outside of the active elements.这种技术适用于 toasts、modals、spinners 等元素,这些元素可以用作活动元素之外的单个 dom 节点。 https://reactjs.org/docs/portals.html . https://reactjs.org/docs/portals.html 。 Here is a great example how to use with: https://blog.yogeshchavan.dev/how-to-use-react-portal-to-create-custom-reusable-toast-component-in-react这是一个很好的例子,如何使用: https://blog.yogeshchavan.dev/how-to-use-react-portal-to-create-custom-reusable-toast-component-in-react
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.