简体   繁体   English

React空白文本节点不能显示为的子级<tbody>

[英]React whitespace text nodes cannot appear as a child of <tbody>

I want to create table with crypto coins symbols but I get this error Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of <tbody>. Make sure you don't have any extra whitespace between tags on each line of your source code. 我想用加密硬币符号创建表,但出现此错误Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of <tbody>. Make sure you don't have any extra whitespace between tags on each line of your source code. Warning: validateDOMNesting(...): Whitespace text nodes cannot appear as a child of <tbody>. Make sure you don't have any extra whitespace between tags on each line of your source code.

import React, { Component } from 'react'

class Main extends Component {
    constructor(props) {
        super(props)

        this.state = {
            symbol: ''
        }
    }

    createTable(coins) {
    }
    getDataFromApi() {
        const url = 'https://api.coingecko.com/api/v3/coins/markets?vs_currency=USD&order=market_cap_desc&per_page=50&page=1&sparkline=false'
        fetch(url)
            .then(response => response.json())
            .then(data => {
                this.setState({
                    symbol: data.map(coin => {
                        return <tr key={coin.symbol}><td>{coin.symbol}</td></tr>;
                    })
                }, () => console.log(this.state.symbol))
            })
    }
    componentDidMount() {
        this.getDataFromApi()
    }
    render() {
        return (
            <div><table><tbody>{this.state.symbol}</tbody></table></div>
        )
    }
}

export default Main

It looks like I want it to but it throws this warning. 看起来像我想要的那样,但是会引发此警告。 In the end I want something like https://www.coingecko.com/en but obviously simpler :) 最后,我想要类似https://www.coingecko.com/en的东西,但显然更简单:)

Found it. 找到了。 Problem was symbol state was initialy string I just converted it to array like so 问题是符号状态是初始字符串,我只是将其转换为数组,如下所示

this.state = {
            symbol: []
        }

文本节点不能作为子节点出现<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]

如何修复警告: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>

暂无
暂无

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

相关问题 文本节点不能作为以下子项出现 <tbody> 。 [阵营] - Text nodes cannot appear as a child of <tbody>. [React] 反应:警告:validateDOMNesting(…):文本节点不能显示为以下子项<tbody> - React: Warning: validateDOMNesting(…): Text nodes cannot appear as a child of <tbody> validateDOMNesting(...):文本节点不能作为子节点出现 - validateDOMNesting(…): Text nodes cannot appear as a child of <tbody> 文本节点不能作为子节点出现 - Text nodes cannot appear as a child of <tbody> 反应:警告:validateDOMNesting(...):空白文本节点不能作为子节点出现 - React: Warning: validateDOMNesting(…): Whitespace text nodes cannot appear as a child of <tr> 语义UI React: <thead> 不能作为的子代出现 <tbody> - Semantic UI React : <thead> 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] 警告:validateDOMNesting(…):<div> 不能作为孩子出现<tbody> - Warning: validateDOMNesting(…): <div> cannot appear as a child of <tbody> span不能是React js中tbody的子代 - span cannot be a child of tbody in react 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>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM