简体   繁体   English

validateDOMNesting(...):文本节点不能作为子节点出现

[英]validateDOMNesting(…): Text nodes cannot appear as a child of <tbody>

i don't know why i'm receiving this warning?我不知道为什么我会收到这个警告? and because of this i can't see updated table..因此我看不到更新的表格..

Warning: validateDOMNesting(...): Text nodes cannot appear as a child of <tbody>.

In some cases I saw that it is about some white spaces and different table tags, but I don't see how this applies in here.在某些情况下,我看到它是关于一些空格和不同的表格标签,但我不明白这在这里如何应用。

TableCom桌康

import { Paper, Table, TableBody, TableCell, TableContainer, TableHead, TableRow } from '@material-ui/core'
import React from 'react'

export const Countries = ({details}) => {
    return (
        <TableContainer component={Paper}>
            <Table aria-label="simple table">
                <TableHead>
                    <TableRow>
                    <TableCell align='right'>Country</TableCell>
                        <TableCell align='right'>Total Infected</TableCell>
                        <TableCell align='right'>Total Recoverd</TableCell>
                        <TableCell align='right'>Total Deaths</TableCell>
                    </TableRow>
                </TableHead>
                <TableBody>
                    {details.length && details.map((detail) => {
                        return (
                      <TableRow>
                          <td key={detail.id}>{detail.cases}</td> : null
                      </TableRow>
                        );
                    })}
                </TableBody>
            </Table>
        </TableContainer>
    )
}

App.js for state and passing props state和传递道具的 App.js

import React, { useEffect, useState } from 'react'
import { Appbar } from './components/pageOne/Appbar'
import { Cards } from './components/pageOne/Cards'
import { fetchData, fetchCountries } from './components/FetchDataFromApi'
import { Countries } from './components/pageOne/TableCom'

function App() {
  const [data, setData] = useState({})
  const [details, setDetails] = useState([])

    useEffect(() => {
      (async () => {
        const fetchAPI = await fetchCountries();
        setDetails(fetchAPI)
      })()
      }, []);

  useEffect(() => {

    (async () => {
      const fetchedData = await fetchData();
      setData(fetchedData)
      
    })()
  }, [])

  return (
    <>
    <Appbar />
    <Cards data = {data} />
    <Countries details = {details} />
    
    </>
  )
}

export default App;


any solution to solve this... codeSandBox解决这个问题的任何解决方案... codeSandBox

The problem is in the way you handle the response which you're getting from the api.问题在于您处理从 api 获得的响应的方式。 You're returning an object instead of an array.您返回的是 object 而不是数组。

https://codesandbox.io/s/silly-dan-7iusu https://codesandbox.io/s/silly-dan-7iusu

Here is a working sandbox, I just get all the data from the response and pass it down.这是一个工作沙箱,我只是从响应中获取所有数据并将其传递下来。 You can also create an array yourself, only with the data you need.您也可以自己创建一个数组,只使用您需要的数据。 But currently you're passing an object.但目前您正在传递 object。

Look at index.js in fetchApi .查看fetchApi中的index.js

如何修复警告:validateDOMNesting(...):<div> 不能作为孩子出现</div><div id="text_translate"><p>我将用户列表作为道具传递给 UserItem 组件,以迭代用户列表并将它们显示在表上。 列表显示正确,我的渲染返回中没有任何 div,但我仍然收到错误:index.js:1446 Warning: validateDOMNesting(...): cannot appear as a child of.</p><p> 尝试了网上找到的许多解决方案,但没有一个有效</p><p>用户管理代码:</p><pre> 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);</pre><p> 用户项目代码:</p><pre> 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;</pre><p> 我希望修复警告信息</p></div> - How to fix Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody>

文本节点不能作为子节点出现<div id="text_translate"><p>为什么我会收到此警告?</p><p> Warning: validateDOMNesting(...): Text nodes cannot appear as a child of &lt;table&gt; 。</p><p> 在某些情况下,我看到它是关于一些空白的,但我不明白这在这里是如何应用的。</p><p> 我的代码:</p><pre> return ( &lt;div className="container"&gt; &lt;div&gt; &lt;h2 style={{color: 'red'}}&gt;Lista de Clientes&lt;/h2&gt; &lt;/div&gt; &lt;br/&gt; &lt;table className="table table-bordered"&gt; &lt;thead className="thead-dark"&gt; &lt;tr&gt; &lt;th&gt;#&lt;/th&gt; &lt;th&gt;Nome&lt;/th&gt; &lt;th&gt;Telefone&lt;/th&gt; &lt;th&gt;E-mail&lt;/th&gt; &lt;th&gt;Detalhar&lt;/th&gt; &lt;th&gt;Excluir&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; { inserirClientes } &lt;/table&gt; &lt;/div&gt; );</pre><p> 在这里创建inserirClientes的循环</p><pre> let inserirClientes = this.state.mensagem if (this.state.retorno) { inserirClientes = ( Object.keys(this.state.clientes).map((key, i) =&gt; &lt;tbody key={key + i} &gt; &lt;Clientes remover={this.removerClienteHandler} clienteInfo={this.state.clientes[key]} clickRemove={() =&gt; this.fetchHandler()} indice={i} /&gt; &lt;/tbody&gt; ) ) }</pre><p> 编辑:</p><p> 我开始在循环中生成&lt;tr&gt;而不是&lt;tbody&gt;并且错误仍然存在,但现在我得到了这个:</p><p> Warning: validateDOMNesting(...): Text nodes cannot appear as a child of &lt;tbody&gt;</p><pre> let inserirClientes = this.state.mensagem if (this.state.retorno) { inserirClientes = ( Object.keys(this.state.clientes).map((key, i) =&gt; ( &lt;tr key={`${key}${i}`} &gt; &lt;Clientes remover={this.removerClienteHandler} clienteInfo={this.state.clientes[key]} clickRemove={() =&gt; this.fetchHandler()} indice={i} /&gt; &lt;/tr&gt; )) ) console.log(inserirClientes) } return ( &lt;div className="container"&gt; &lt;div&gt; &lt;h2 style={{color: 'red'}}&gt;Lista de Clientes&lt;/h2&gt; &lt;h4 style={{color: 'red'}}&gt;OBS.: Verificar se é a melhor maneira de se criar tal tabela. Talvez seja possível criar um componente para isso&lt;/h4&gt; &lt;/div&gt; &lt;br/&gt; &lt;table className="table table-bordered"&gt; &lt;thead className="thead-dark"&gt; &lt;tr&gt; &lt;th&gt;#&lt;/th&gt; &lt;th&gt;Nome&lt;/th&gt; &lt;th&gt;Telefone&lt;/th&gt; &lt;th&gt;E-mail&lt;/th&gt; &lt;th&gt;Detalhar&lt;/th&gt; &lt;th&gt;Excluir&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;{inserirClientes}&lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; );</pre><p> 知道如何解决这个问题吗?</p></div>[反应JS]<table> </table> - Text nodes cannot appear as a child of <table> [ReactJS]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 反应:警告:validateDOMNesting(…):文本节点不能显示为以下子项<tbody> - React: Warning: validateDOMNesting(…): Text nodes cannot appear as a child of <tbody> 警告:validateDOMNesting(…):<div> 不能作为孩子出现<tbody> - Warning: validateDOMNesting(…): <div> cannot appear as a child of <tbody> 文本节点不能作为子节点出现 - Text nodes cannot appear as a child of <tbody> 文本节点不能作为以下子项出现 <tbody> 。 [阵营] - Text nodes cannot appear as a child of <tbody>. [React] 反应:警告:validateDOMNesting(...):空白文本节点不能作为子节点出现 - React: Warning: validateDOMNesting(…): Whitespace text nodes cannot appear as a child of <tr> 如何修复警告:validateDOMNesting(...):<div> 不能作为孩子出现</div><div id="text_translate"><p>我将用户列表作为道具传递给 UserItem 组件,以迭代用户列表并将它们显示在表上。 列表显示正确,我的渲染返回中没有任何 div,但我仍然收到错误:index.js:1446 Warning: validateDOMNesting(...): cannot appear as a child of.</p><p> 尝试了网上找到的许多解决方案,但没有一个有效</p><p>用户管理代码:</p><pre> 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);</pre><p> 用户项目代码:</p><pre> 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;</pre><p> 我希望修复警告信息</p></div> - How to fix Warning: validateDOMNesting(...): <div> cannot appear as a child of <tbody> React空白文本节点不能显示为的子级<tbody> - React whitespace text nodes cannot appear as a child of <tbody> 如何修复 validateDOMNesting(...): 不能作为子级出现。 并且列表中的每个孩子都应该有一个唯一的“关键”道具 - How to fix validateDOMNesting(…): <td> cannot appear as a child of <tbody>. and Each child in a list should have a unique “key” prop validateDOMNesting(...):<tr> 不能作为孩子出现<div> - validateDOMNesting(...): <tr> cannot appear as a child of <div> 文本节点不能作为子节点出现<div id="text_translate"><p>为什么我会收到此警告?</p><p> Warning: validateDOMNesting(...): Text nodes cannot appear as a child of &lt;table&gt; 。</p><p> 在某些情况下,我看到它是关于一些空白的,但我不明白这在这里是如何应用的。</p><p> 我的代码:</p><pre> return ( &lt;div className="container"&gt; &lt;div&gt; &lt;h2 style={{color: 'red'}}&gt;Lista de Clientes&lt;/h2&gt; &lt;/div&gt; &lt;br/&gt; &lt;table className="table table-bordered"&gt; &lt;thead className="thead-dark"&gt; &lt;tr&gt; &lt;th&gt;#&lt;/th&gt; &lt;th&gt;Nome&lt;/th&gt; &lt;th&gt;Telefone&lt;/th&gt; &lt;th&gt;E-mail&lt;/th&gt; &lt;th&gt;Detalhar&lt;/th&gt; &lt;th&gt;Excluir&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; { inserirClientes } &lt;/table&gt; &lt;/div&gt; );</pre><p> 在这里创建inserirClientes的循环</p><pre> let inserirClientes = this.state.mensagem if (this.state.retorno) { inserirClientes = ( Object.keys(this.state.clientes).map((key, i) =&gt; &lt;tbody key={key + i} &gt; &lt;Clientes remover={this.removerClienteHandler} clienteInfo={this.state.clientes[key]} clickRemove={() =&gt; this.fetchHandler()} indice={i} /&gt; &lt;/tbody&gt; ) ) }</pre><p> 编辑:</p><p> 我开始在循环中生成&lt;tr&gt;而不是&lt;tbody&gt;并且错误仍然存在,但现在我得到了这个:</p><p> Warning: validateDOMNesting(...): Text nodes cannot appear as a child of &lt;tbody&gt;</p><pre> let inserirClientes = this.state.mensagem if (this.state.retorno) { inserirClientes = ( Object.keys(this.state.clientes).map((key, i) =&gt; ( &lt;tr key={`${key}${i}`} &gt; &lt;Clientes remover={this.removerClienteHandler} clienteInfo={this.state.clientes[key]} clickRemove={() =&gt; this.fetchHandler()} indice={i} /&gt; &lt;/tr&gt; )) ) console.log(inserirClientes) } return ( &lt;div className="container"&gt; &lt;div&gt; &lt;h2 style={{color: 'red'}}&gt;Lista de Clientes&lt;/h2&gt; &lt;h4 style={{color: 'red'}}&gt;OBS.: Verificar se é a melhor maneira de se criar tal tabela. Talvez seja possível criar um componente para isso&lt;/h4&gt; &lt;/div&gt; &lt;br/&gt; &lt;table className="table table-bordered"&gt; &lt;thead className="thead-dark"&gt; &lt;tr&gt; &lt;th&gt;#&lt;/th&gt; &lt;th&gt;Nome&lt;/th&gt; &lt;th&gt;Telefone&lt;/th&gt; &lt;th&gt;E-mail&lt;/th&gt; &lt;th&gt;Detalhar&lt;/th&gt; &lt;th&gt;Excluir&lt;/th&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt;{inserirClientes}&lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; );</pre><p> 知道如何解决这个问题吗?</p></div>[反应JS]<table> </table> - Text nodes cannot appear as a child of <table> [ReactJS]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM