I have built a React component that gets data from an Azure Cosmos database (JSON object) and displays this in a card like structure in a web app.
The JSON contains several top-level key/value pairs and then a 'tickets' node that can contain a variable amount of children which are numbered 1..n with several key/value pairs as sub-children, see simplified example below:
{
"id": "541f6100-ab47-48ed-a415-92a509c4b9bd",
"tickets": {
"1": {
"ticketID": "d10400e3-ccea-4592-aef1-19005f9c91d8"
},
"2": {
"ticketID": "05a7e6d4-a4ff-4402-bb42-52fdc7dbf72b"
},
"3": {
"ticketID": "e7e332f2-be7b-47b8-a864-2244d720a12e"
}
}
}
My React component is roughly structured as follows (simplified for readability):
const Cards = ({ CosmoData }) => {
return (
<div>
{CosmoData.map((card) => (
<div>
<div key={card.id}>
<div>
<div>
<table>
<tbody>
<tr key={card.tickets}>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
))}
</div>
);
}
The data is fed to the client component by a server component as follows:
export default async function Page() {
// Fetch data directly in a Server Component
const CosmoData = await getCards();
// Forward fetched data to your Client Component
return <Cards CosmoData={CosmoData} />;
};
So CosmoData.map() method makes the top-level key/value pair accessible. However, I can't seem to find how to work with the child nodes, specifically counting the number of children so that the index can be used for the key property.
Am I supposed to somehow nest .map methods?