after I fetch the array from API, I use the data to build a table using a child component:
<tbody>
{data.map((item, index) => (
<Row key={index} item={item} deleteMe={deleteChild}/>
))}
</tbody>
How can I use the above index in the Row component? The child is using the array data like this:
return (
<tr>
<td>**Here should be the index**</td>
<td>{item.exerciseName}</td>
<td>{item.instructions}</td>
</tr>
);
UPDATE
Thank you for all the answers, managed to show the index using your suggestions.
3
Answers
The key property is used by React under the hood and is not exposed to you.
You will have to make another prop passed on to the child and access it as you like.
You can use props to do so,
and inside your child component you can use it by
props.index
You can pass the index as a separate prop.
Then, it can be directly accessed inside the
Row
component.Working example: