簡體   English   中英

文本節點不能作為子節點出現<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]

為什么我會收到此警告?

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

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

我的代碼:

        return (
            <div className="container">
                <div>
                    <h2 style={{color: 'red'}}>Lista de Clientes</h2>
                </div>
                <br/>

                <table className="table table-bordered">
                    <thead className="thead-dark">
                        <tr>
                            <th>#</th>
                            <th>Nome</th>
                            <th>Telefone</th>
                            <th>E-mail</th>
                            <th>Detalhar</th>
                            <th>Excluir</th>
                        </tr>
                    </thead>
                    { inserirClientes }
                </table>
            </div>
        );

在這里創建inserirClientes的循環

        let inserirClientes = this.state.mensagem

        if (this.state.retorno) {

            inserirClientes = (
                Object.keys(this.state.clientes)
                    .map((key, i) =>
                                <tbody key={key + i}  >
                                    <Clientes 
                                        remover={this.removerClienteHandler} 
                                        clienteInfo={this.state.clientes[key]} 
                                        clickRemove={() => this.fetchHandler()}
                                        indice={i}
                                    />
                                </tbody>
                        )
            )
        }

編輯:

我開始在循環中生成<tr>而不是<tbody>並且錯誤仍然存在,但現在我得到了這個:

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

let inserirClientes = this.state.mensagem

    if (this.state.retorno) {

        inserirClientes = (
            Object.keys(this.state.clientes)
                .map((key, i) => (
                            <tr key={`${key}${i}`}  >
                                <Clientes 
                                    remover={this.removerClienteHandler} 
                                    clienteInfo={this.state.clientes[key]} 
                                    clickRemove={() => this.fetchHandler()}
                                    indice={i}
                                />
                            </tr>
                    ))
        )
        console.log(inserirClientes)
    }

    return (
        <div className="container">
            <div>
                <h2 style={{color: 'red'}}>Lista de Clientes</h2>
                <h4 style={{color: 'red'}}>OBS.: Verificar se é a melhor maneira de se criar tal tabela. Talvez seja possível criar um componente para isso</h4>
            </div>
            <br/>

            <table className="table table-bordered">
                <thead className="thead-dark">
                    <tr>
                        <th>#</th>
                        <th>Nome</th>
                        <th>Telefone</th>
                        <th>E-mail</th>
                        <th>Detalhar</th>
                        <th>Excluir</th>
                    </tr>
                </thead>
                <tbody>{inserirClientes}</tbody>
            </table>
        </div>
    );

知道如何解決這個問題嗎?

答案在於inserirClientes的初始值。

Since you've initialized inserirClientes to be equal to this.state.mensagem , this component's first render will use that as the initial value, because Object.keys(this.state.clientes).map() has not yet had a chance to跑。

我猜這些是您最初的 state 值?

state = {
  mensagem: 'initial message',
  retorno: false,

  // other values...
}

如果是這樣, inserirClientes在第一次渲染時具有'initial message'的值,並且您正在有效地執行此操作:

<tbody>{'initial message'}</tbody>


解決方案

由於您可能希望在數據加載之前顯示初始消息,我們可以簡單地在初始消息周圍提供必要的標記,使其在<table>中有效。

// since this will be a child of <tbody>, start with valid <tr> markup
let inserirClientes = <tr><td>{this.state.mensagem}</td></tr>

// if data has returned, replace initial message with array of <tr>s
if (this.state.retorno) {
  inserirClientes =
    Object.keys(this.state.clientes).map((key, i) => (
      <tr key={`${key}${i}`}  >
        <Clientes 
          remover={this.removerClienteHandler} 
          clienteInfo={this.state.clientes[key]} 
          clickRemove={() => this.fetchHandler()}
          indice={i}
        />
      </tr>
    ))
}

console.log(inserirClientes)

return (
  <div className="container">
    <div>
      <h2 style={{color: 'red'}}>Lista de Clientes</h2>
      <h4 style={{color: 'red'}}>OBS.: Verificar se é a melhor maneira de se criar tal tabela. Talvez seja possível criar um componente para isso</h4>
    </div>
    <br/>

    <table className="table table-bordered">
      <thead className="thead-dark">
        <tr>
          <th>#</th>
          <th>Nome</th>
          <th>Telefone</th>
          <th>E-mail</th>
          <th>Detalhar</th>
          <th>Excluir</th>
        </tr>
      </thead>
      <tbody>{inserirClientes}</tbody>
    </table>
  </div>
)

HTML 中表的正確結構是

<table>
 <thead>
   <tr>
     <th>heading 1</th>
     <th>heading 2</th>
   </tr>
 </thead>
 <tbody>
   <tr>
     <td>col 1</td>
     <td>col 2</td>
   </tr>
 </tbody>
</table>

因此,您需要在<tr>中添加一個<td>以使其成為有效的 DOM 結構

inserirClientes =
      <tr>
        {Object.keys(this.state.clientes).map((key, i) => (
          <td key={`${key}${i}`}>
            <Clientes
              remover={this.removerClienteHandler}
              clienteInfo={this.state.clientes[key]}
              clickRemove={() => this.fetchHandler()}
              indice={i}
            />
          </td>
        ))}
      </tr>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM