【问题标题】:Import an array from the redux store to a table. React,js将数组从 redux 存储导入到表中。反应,js
【发布时间】:2021-08-10 01:46:21
【问题描述】:

我正在尝试从 redux 存储导入数据并在表格中呈现。

我尝试了很多次,但总是收到此错误:对象作为反应子项无效(找到:带有键 {} 的对象)。如果您打算渲染一组子项,请改用数组。

我认为我应该使用 .map 方法,但我不知道何时使用 useSelector 获取数据

这是我的 redux 商店

我要渲染数据的表格

<table className="table">
        <thead>
            <tr>
                <th style={{ width: 100 }}>ID</th>
                <th style={{ width: 150 }}>Nombre</th>
                <th style={{ width: 150 }}>Precio</th>
                <th style={{ width: 150 }}>Stock</th>
            </tr>
        </thead>
        <tbody>
            {
                <tr>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            }
        </tbody>
    </table>

我使用了一个 useSelector 从存储中获取数据

const { body } = useSelector( state => state.coffe );

但是现在我该如何渲染数据呢?

还是我做错了什么?

有人可以帮我弄清楚吗?感谢您的宝贵时间!

【问题讨论】:

    标签: arrays reactjs redux react-redux store


    【解决方案1】:

    首先检查体是返回数据。如果是,请使用此代码

    const Table = () => {
        const { body } = useSelector( state => state.coffe );
        return (
            <table className="table">
                <thead>
                    <tr>
                        <th style={{ width: 100 }}>ID</th>
                        <th style={{ width: 150 }}>Nombre</th>
                        <th style={{ width: 150 }}>Precio</th>
                        <th style={{ width: 150 }}>Stock</th>
                    </tr>
                </thead>
                <tbody>
                    {body && body.map( b => 
                        <tr key={b.id}>
                            <td>{b.nombre}</td>
                            <td>{b.precio}</td>
                            <td>{b.stock}</td>
                        </tr>
                    )}
                </tbody>
            </table>
        )
    }
    

    导出默认表格;

    要渲染数据,您需要使用地图功能。

    【讨论】:

    • 非常感谢!!我真的很感激
    猜你喜欢
    • 1970-01-01
    • 2022-08-16
    • 2020-05-21
    • 2020-11-05
    • 2019-11-20
    • 2021-05-06
    • 1970-01-01
    • 2020-01-06
    • 2021-12-25
    相关资源
    最近更新 更多