“Nächste JS GetstaticPaths” Code-Antworten

GetstaticPaths in NextJs

//pages\news\[slug].tsx
export async function getStaticPaths() {
  const res = await fetch(`${baseUrl}/wp-json/wp/v2/posts`)
  const posts = await res.json()
  const paths = posts.map(({ slug }) => ({ params: { slug: `${slug}` } }))
  return {
    paths,
    fallback: true,
    //The paths that have not been generated at build time will not result in a 404 page. 
    //Instead, fallback: true This will be used to automatically render
    //the page with the required props.
  }
}

export async function getStaticProps(context) {
  const resPost = await fetch(`${baseUrl}/wp-json/wp/v2/posts?slug=${context.params.slug}`)

  const post = await resPost.json()

  return {
    props: {
      post,
    },
    revalidate: 10,
  }
}
Shirshak kandel

Nächste JS GetstaticPaths

// create a post folder and index.js file in pages/posts folder
import Link from 'next/link';
const Posts = ({ posts }) => (
  <div>
    {
      posts.map(post => (
        <div>
          <Link href={`/posts/${post.id}`}><a style={{fontSize:'20px',textDecoration: 'underline'}}>{post.title}</a></Link>
          <p>{post.body}</p>
        </div>
      ))
    }
  </div>
)

export async function getServerSideProps() {
  const res = await fetch(`https://jsonplaceholder.typicode.com/posts`)
  const posts = await res.json()
  return { props: { posts } }
}
export default Posts;



// then add another file in 
pages\posts\[id].js

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    // We'll pre-render only these paths at build time.
    // { fallback: false } means other routes should 404.
    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}
export default Post
CodePadding

Nächste JS GetstaticPaths

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    // Call an external API endpoint to get posts
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}

export default Post
CodePadding

Nächste JS GetstaticPaths

// create a post folder and index.js file in pages/posts folder
import Link from 'next/link';
const Posts = ({ posts }) => (
  <div>
    {
      posts.map(post => (
        <div>
          <Link href={`/posts/${post.id}`}><a style={{fontSize:'20px',textDecoration: 'underline'}}>{post.title}</a></Link>
          <p>{post.body}</p>
        </div>
      ))
    }
  </div>
)

export async function getServerSideProps() {
  const res = await fetch(`https://jsonplaceholder.typicode.com/posts`)
  const posts = await res.json()
  return { props: { posts } }
}
export default Posts;



// then add another file in 
pages\posts\[id].js

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    // We'll pre-render only these paths at build time.
    // { fallback: false } means other routes should 404.
    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}
export default Post
CodePadding

Nächste JS GetstaticPaths

function Post({ post }) {
    // Render post...
    return (
        <div>
            <h1>{post.title}</h1>
            <p>{post.body}</p>
        </div>
    )
}

export async function getStaticPaths() {
    // Call an external API endpoint to get posts
    const res = await fetch('https://jsonplaceholder.typicode.com/posts')
    const posts = await res.json()
    const paths = posts.map((post) => ({
        params: { id: post.id.toString() },
    }))

    return { paths, fallback: true }
}

export async function getStaticProps({ params }) {
    const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${params.id}`)
    const post = await res.json()
    return { 
        props: { post },
        revalidate: 10,
    }
}

export default Post
CodePadding

Ähnliche Antworten wie “Nächste JS GetstaticPaths”

Fragen ähnlich wie “Nächste JS GetstaticPaths”

Weitere verwandte Antworten zu “Nächste JS GetstaticPaths” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen