Note that this question is tagged next.js. Familiarity with Next.js is essential for understanding the relationship between the getStaticProps
and Home
functions in the code below.
export async function getStaticProps() {
const dummyData = [
{
id: '1',
name: 'john'
},
{
id: '2',
name: 'Tom'
}
];
return {
props: { data:dummyData }
};
}
export default function Home({ data }) {
console.log(data);
return (
<ul>
<li>USER</li>
{data && data.map((user) => (
<li key={user.id}>
{user.name}
</li>
))}
</ul>
);
}
I try to send data to the Home
function using getStaticProps
, but when I console.log
it, it shows undefined
.
I want to receive the data from getStaticProps().
2
Answers
getStaticProps() is returning a promise. You can’t get data synchronously.
Refer this more details
You just declared the Home component, but you haven’t called and passed the value to it yet.
At place you call component Home, you must pass value for data like this