How to get all the params values in getStaticProps

Viewed 32

I am trying to figure out a way to map both slugs and paths in order to get all their values in the params : This is my code :

export async function getStaticPaths() {
  const slugs = await client.fetch(
    `*[_type == "comic" && defined(slug.current)][].slug.current`
  )
  const paths = await client.fetch(
    `*[_type == "comic"][].chapters[].number`
  )

  return {
    paths: paths.map((number : any) => ({params: {number: number, slug: slugs}})), //Here I have to change this piece of code to get number and slug as params
    fallback: false,
  }
}
1 Answers

if you want to get data you need to send it by props...

export async function getStaticPaths() {
    const slugs = await client.fetch(
      `*[_type == "comic" && defined(slug.current)][].slug.current`
    )
    const paths = await client.fetch(
      `*[_type == "comic"][].chapters[].number`
    )
  
    return {
      paths: paths.map((number : any) => ({params: {number: number, slug: slugs}})), //Here I have to change this piece of code to get number and slug as params
      fallback: false,
      props: {
        paths: paths,
        slugs: slugs
      }
    }
}

you can capture like this...

function Page ({ paths, slugs }) {
    return (
      <div>
      </div>
    )
}

READ CAREFULLY THE DOCUMENTATION: https://nextjs.org/docs/basic-features/data-fetching/get-static-props

Related