Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

next js getserversideprops

export async function getServerSideProps(context) {
  return {
    props: {}, // will be passed to the page component as props
  }
}
Comment

nextjs getserversideprops

function Page({ data }) {
  // Render data...
}

// This gets called on every request
export async function getServerSideProps() {
  // Fetch data from external API
  const res = await fetch(`https://.../data`)
  const data = await res.json()

  // Pass data to the page via props
  return { props: { data } }
}

export default Page
Comment

getServersideprops in nextjs

export async function getStaticPaths() {
  const res = await fetch(`${baseUrl}/wp-json/wp/v2/posts`)
  const posts = await res.json()
  //paths is array of objects which is contains one params property with id or slug
  const paths = posts.map(({ slug }) => ({ params: { slug: `${slug}` } }))

  return {
    paths,
    //fallback true mean there is no slug in build time then it will not shown 404 error 
    // and fetch data from server and show it
    fallback: true,
  }
}
Comment

can we use getServersideprops in any component in next.js

You cannot use getServerSideProps in non-page components. You can either pass the prop from Home to HomeSection or create a context so the value can be available globally from the component tree

getServerSideProps can only be exported from a page. You can’t export it from non-page files.

https://nextjs.org/docs/basic-features/data-fetching#only-allowed-in-a-page-2
Comment

PREVIOUS NEXT
Code Example
Javascript :: remove duplicates from array 
Javascript :: jquery checkbox unchecked 
Javascript :: mongodb update many 
Javascript :: tailwind hover dont work 
Javascript :: javascript get now date yyyy-mm-dd 
Javascript :: How to remove title in material-table 
Javascript :: axios x-api-key for all 
Javascript :: prevent multiple form submissions javascript 
Javascript :: convert elements to array javascript 
Javascript :: jquery validation plugin google recaptcha 
Javascript :: react-native android build apk 
Javascript :: “javascript sleep 1 second” 
Javascript :: how to limit input type max length 
Javascript :: javascript assignment operators 
Javascript :: slickcdn 
Javascript :: how to get seconds in timstamps js 
Javascript :: delete element 
Javascript :: how to handle navigation between multiple stack react native 
Javascript :: js onchange input value event listene 
Javascript :: axios set body 
Javascript :: vuex use state in action 
Javascript :: javascript on keypu 
Javascript :: conditional field validation with Yup 
Javascript :: jquery select2 how to make dont close after select 
Javascript :: heroku scripts 
Javascript :: how to access key of object in javascript 
Javascript :: javascript run command 
Javascript :: nodejs catch uncaught exception 
Javascript :: javascript sum array values 
Javascript :: jquery ajax on fail 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =