I need to create some cards that will have the content based on data returned from an API. However, I haven’t found ways to do this when the website is being rendered, and the ones I’ve tried haven’t worked. The div will contain the cards, but how can i pass the "packages" as properties to the component?
import { GetPackages, SetWebstoreIdentifier } from "tebex_headless";
SetWebstoreIdentifier("tv5y-6b682b0fe1661c45c836b56fb7244d3d840d18c7");
async function test() {
const packages = await GetPackages();
console.log(packages[0]);
}
test();
export function CardBox() {
return (
<div
className="
grid gap-6 place-items-center
sm:grid-cols-2
lgg:grid-cols-4
"
></div>
);
}
I tried to use an
{Array.from({length:4}).map(() => { <Card //The values returned from the package would come here /> })}
but, in the ways I’ve tried, I couldn’t pass the package values into the component.
2
Answers
The steps that you need to do:
Let’s imagine that this the data returned from your API. And let’s call it packageData.
Here is the edited version of CardBox:
And here is how you’ll create your Card component. You can edit it according to the data coming from your API.
Card component:
In Next.js (App Router) you can create
async
component (only works withserver components
i.e. not using"use client"
).so you can fetch your data directly in your component then render it as you like.
(the below code assumes that
const packages = await GetPackages();
will return data inarray-like
format)