9/17/22

how to fetch data at nextJS

well - main thing application should do - is to display data.
so, you can fetch data from external source very simple:

export const getStaticProps = async () => {
  const res = await fetch(`https://jsonplaceholder.typicode.com/users`)
  const users = await res.json();
  return {
    props: { users }
  }
}

export default function Home({ users }) {
  return <div>{users.map(({id, name}) => <div key={id}>{name}</div>)}</div>
}
The difference here - is that now you have no 'loading' time when page is empty.
That is because data loading happens on the server side

No comments:

Post a Comment

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...