-3

Normally React JS Using useEffect for Client Side Data Fetching but I want to fetch Data in server side in Next Js that time the code also reusable any one can clear my doubt

Dinesh V
  • 1
  • 2

1 Answers1

0
function MyPageComponent({ data }) {
  // You page component that will be displayed on frontend
  // Here you can use useEffects that will be executed on the frontend as the page loads
}

// This gets called on every request on the BackendOnly!
export async function getServerSideProps() {
  // Do your server logic here, like fetching some api to get an initial state
  const res = await fetch(`site url`)
  const data = await res.json()

  // Pass data to your page component via props
  // This will be accessible on the frontend
  return { props: { data } }
}

export default MyPageComponent
  • When Page Load Data Fetching use getInitialProps or getServerSideProps – Dinesh V Oct 17 '22 at 14:24
  • this is the link actually i didn't understood please go through and explain it please https://stackoverflow.com/questions/64321263/nextjs-app-ssg-getinitialprops-vs-getstaticprops-and-how-am-i-supposed-to-st – Dinesh V Oct 17 '22 at 14:33
  • this code put _app.js or index.js file from pages directory – Dinesh V Oct 17 '22 at 14:41