簡體   English   中英

validateDOMNesting(...):文本節點不能作為子節點出現

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

我不知道為什么我會收到這個警告? 因此我看不到更新的表格..

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

在某些情況下,我看到它是關於一些空格和不同的表格標簽,但我不明白這在這里如何應用。

桌康

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

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;


解決這個問題的任何解決方案... codeSandBox

問題在於您處理從 api 獲得的響應的方式。 您返回的是 object 而不是數組。

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

這是一個工作沙箱,我只是從響應中獲取所有數據並將其傳遞下來。 您也可以自己創建一個數組,只使用您需要的數據。 但目前您正在傳遞 object。

查看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> 警告:validateDOMNesting(…):<div> 不能作為孩子出現<tbody> 文本節點不能作為子節點出現 文本節點不能作為以下子項出現 <tbody> 。 [陣營] 反應:警告:validateDOMNesting(...):空白文本節點不能作為子節點出現 如何修復警告: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> React空白文本節點不能顯示為的子級<tbody> 如何修復 validateDOMNesting(...): 不能作為子級出現。 並且列表中的每個孩子都應該有一個唯一的“關鍵”道具 validateDOMNesting(...):<tr> 不能作為孩子出現<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>
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM