首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用getServerSideProps

如何使用getServerSideProps
EN

Stack Overflow用户
提问于 2022-03-01 03:24:19
回答 1查看 1.1K关注 0票数 0

我是next.js的新手。最近,我了解了next.js中的预渲染技术,并且对如何从getServerSideProps更新道具数据感到困惑。

_app.tsx

代码语言:javascript
复制
import type { AppProps } from 'next/app';

function MyApp({ Component, pageProps }: AppProps) {
  return <Component {...pageProps} />;
}

export default MyApp;

/页面/任务/list.tsx

EN

回答 1

Stack Overflow用户

发布于 2022-03-01 10:34:34

下面是一个关于如何使用GetServerSideProps的示例:

代码语言:javascript
复制
//'pages/index.js'

// Extraction des articles reçus via les props
// Props extraction 
export default function Home({articles}) {
    return (
        <div>
            {articles.map((el, key) => (
                <div key={key}>
                    <p>{JSON.stringify(el)}</p>

                </div>
            ))}
        </div>
    )
}
export const getServerSideProps = async (context) => {

    // Fetching de notre route
    // API fetching
    const res = await fetch('http://localhost:3000/api/yourApiRoutes')

    // Récupération des données de notre requête
    // Cast value to json object
    const articles = await res.json()

    return {
        // Approvisionnement des props de notre page
        // Sending articles to page
        props: {articles: articles}
    }
}

希望我能帮忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71303390

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档