A
A
AlexCruel2020-11-08 09:22:13
React
AlexCruel, 2020-11-08 09:22:13

Rendering properties of array objects?

When the button is clicked, [{}, {}, {}] are unloaded. Each object has properties.

const [list, listRender] = useState(<tr></tr>);
------------------------------------------------
const data = await request('api/search/test', 'POST', { ...form });

listRender(data.map((item, index) => 
    <td key={index}>{item}</td>
));


Render to the property sheet of the object.
<tbody>
           <tr>{list}</tr>
</tbody>


The error pops up "Error: Objects are not valid as a React child (found: object with keys {_id, date, studID, fullName, facility, typeFacility, time}). If you meant to render a collection of children, use an array instead ."

How to render the properties of each object into a separate table row?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AlexCruel, 2020-11-08
@AlexCruel

listRender(data.map((item, index) => 
                <tr key={index}>
                    <td>{item.date}</td>
                    <td>{item.studID}</td>
                    <td>{item.fullName}</td>
                    <td>{item.facility}</td>
                    <td>{item.typeFacility}</td>
                    <td>{item.time}</td>
                </tr>
            ));

<tbody>
      {list}
</tbody>

V
Vladimir, 2020-11-08
@HistoryART

If I'm not mistaken, and I often am. You need it like this:

listRender(data.map((item, index) => 
   <td key={index}>{item}</td>
));

// Не занимался подобными извращениями - map возвращает новый массив, forEach просто перебирает, советую подтянуть в этом плане

<tr>{list.map(el => el)}</tr>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question