validateDOMNesting(…): cannot appear as a child of

前端 未结 4 1217
离开以前
离开以前 2021-01-18 07:29

I\'m trying to get this work:

Orders:

render () {
    const orders = this.state.orders.map((o         


        
相关标签:
4条回答
  • 2021-01-18 07:59

    Or more elegant:

    render () {
        return (
            <table>
                <tbody>
                {this.state.orders.map((order, index) => (
                	return (<OrderRow order={order} key={index} />);
                )}
                </tbody>
            </table>
        );
    }

    0 讨论(0)
  • 2021-01-18 08:00

    Something like this:

    render () {
        return (
            <table>
                <tbody>
                {this.state.orders.map((order, index) => {
    		        <tr index={index}>
    		            <td>{order.number}</td>
    		            <td>{order.products}</td>
    		            <td>{order.shippingDate}</td>
    		            <td>{order.status}</td>
    		        </tr>
    		    }}
                </tbody>
            </table>
        );
    }

    0 讨论(0)
  • 2021-01-18 08:10

    It renders tr into div use .map() instead :)

    0 讨论(0)
  • 2021-01-18 08:25

    Wrap your OrderRow <tr> in <tbody> as explained in issue here, Browsers need the <tbody> tag. If it is not in your code, then the browser will automatically insert it. This will work fine on first render, but when the table gets updated, then the DOM tree is different from what React expects. This can give strange bugs, therefore React warns you to insert the <tbody>.

    OrderRow

    render () {
        return (
           <table>
            <tbody>
             <tr>
                <td>{this.props.order.number}</td>
                <td>{this.props.order.products}</td>
                <td>{this.props.order.shippingDate}</td>
                <td>{this.props.order.status}</td>
             </tr>
            </tbody>
           </table>
        );
    }
    
    0 讨论(0)
提交回复
热议问题