0

I am trying to import data from the redux store and render in a table.

I tried a lot of times but always get this error: objects are not valid as a react child (found: object with keys {}). if you meant to render a collection of children, use an array instead.

I think i should use a .map method but i don't know how when i get the data with an useSelector

This is my redux store

Redux store

The table which I want to render the data

<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>

I used an useSelector to get the data from the store

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

But now how can i render the data?

Or i'm doing something wrong?

Could someone help me figure it out? Thanks for your time!!

1 Answer 1

2

First of all check body is returning the data. If yes use this code

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>
    )
}

export default Table;

To render the data you need to use the map function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much!! i really appreciate it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.