【问题标题】:Uncaught Error: React.Children.only expected to receive a single React element child未捕获的错误:React.Children.only 期望接收单个 React 元素子项
【发布时间】:2021-07-03 10:02:03
【问题描述】:

我正在尝试使用 antd 库创建一个 web 表,但我遇到了一个错误

“react.development.js:1251 未捕获的错误:React.Children.only 预计会收到一个 React 元素子”。我完全陷入困境,无法找到解决方案。有人可以帮忙吗?

import React, { Component } from 'react';
import { Table, Divider, Tag } from 'antd';
import Link from 'next/link';
import { connect } from 'react-redux';

import { getOrdersByUser } from '../../../../store/profile/action';

class TableInvoices extends Component {
    constructor(props) {
        super(props);
        this.state = {};
    }

    componentDidMount() {
        this.props.dispatch(getOrdersByUser('buyer1@gmail.com'));
    }

    render() {
        

        // const tableData = [
        //     {
        //         id: '1',
        //         invoice_id: '500884010',
        //         product_title: 'Marshall Kilburn Portable Wireless Speaker',
        //         invoice_date: '20-1-2020',
        //         cost: '42.99',
        //         status: 'Successful delivery',
        //     },
        //     {
        //         id: '2',
        //         invoice_id: '593347935',
        //         product_title: 'Herschel Leather Duffle Bag In Brown Color',
        //         invoice_date: '20-1-2020',
        //         cost: '199.99',
        //         status: 'Cancel',
        //     },
        // ];
        let tableData = [];
        const { userOrderHistory } = this.props;
        if (
            typeof userOrderHistory != 'undefined' &&
            userOrderHistory.length > 0
        ) {
            tableData = this.props.userOrderHistory.map((row) => ({
                id: row.id,
                invoiceID: row.invoice_id,
                productTitle: row.product_title,
                invoiceDate: row.invoice_date,
                cost: row.cost,
                status: row.status,
            }));
        }
        const tableColumn = [
            {
                title: 'Id',
                dataIndex: 'invoiceID',
                rowKey: 'invoiceID',
                key: 'invoiceID',
                width: '120px',
                render: (text, record) => (
                    <Link href="/account/invoice-detail">
                        {record.invoiceID}
                    </Link>
                ),
            },
            {
                title: 'Title',
                dataIndex: 'productTitle',
                rowKey: 'productTitle',
                key: 'productTitle',
            },
            {
                title: 'Date',
                rowKey: 'invoiceDate',
                dataIndex: 'invoiceDate',
                key: 'invoiceDate',
                width: '120px',
            },
            {
                title: 'Amount',
                rowKey: 'cost',
                dataIndex: 'cost',
                key: 'cost',
                render: (text, record) => (
                    <span className="text-right">${record.cost}</span>
                ),
            },
            {
                title: 'Status',
                key: 'status',
                dataIndex: 'status',
                rowKey: 'status',
                width: '150px',
                render: (text, record) => (
                    <span className="text-right">{record.status}</span>
                ),
            },
        ];
        console.log('tableColumn');
        return (
            <Table
                columns={tableColumn}
                dataSource={tableData}
                rowKey={(record) => record.id}
            />
        );
    }
}

const mapStateToProps = (state) => {
    return state.profile;
};
export default connect(mapStateToProps)(TableInvoices);

【问题讨论】:

    标签: javascript reactjs next.js antd


    【解决方案1】:

    connect 和 Redux Provider 都需要单个子组件,所以要检查的一件事是:根据我在 antd 文档上看到的,表组件可以返回多个子组件,尝试包装片段中的表格组件。

    否则,您确定它来自此文件吗?
    也许问题来自 Redux Provider,在这种情况下:
    React.children.only expected to receive a single react element child Navigator 会有所帮助

    另外,为了以防万一,即使这对我来说非常令人惊讶,您是否尝试过删除括号和返回的实际组件之间的空白?也许有一个不间断的空间?

    return <Table
      columns={tableColumn}
      dataSource={tableData}
      rowKey={(record) => record.id}
    />
    

    【讨论】:

    • 如果 {tableData} 在 React 组件中被硬编码,代码可以正常工作...当我尝试从 API 读取它时,它会抛出错误...我已经将 API 的输出与硬编码的表数据进行了比较...完全一样。
    • 不幸的是,如果不能检查事情就很难说,也许你可以添加一个jsfiddle让我们探索问题的根源?
    猜你喜欢
    • 2020-08-27
    • 2017-10-29
    • 2017-08-30
    • 2019-07-12
    • 2020-09-14
    • 2020-11-07
    • 2018-05-23
    • 2021-06-05
    • 2020-08-22
    相关资源
    最近更新 更多