skip to Main Content

Note that this question is tagged . 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


  1. getStaticProps() is returning a promise. You can’t get data synchronously.

    let data =  await getStaticProps()
    

    Refer this more details

    Login or Signup to reply.
  2. 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

    <App>
      <Home data={[1,2,3,4]} />
    </App>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search