【问题标题】:use getStaticProps in component在组件中使用 getStaticProps
【发布时间】:2021-09-09 22:08:53
【问题描述】:

我用 nextjs 和 typescript 开始了一个项目。我有一个主要组件,我在 index.js 页面中调用它我在主要组件中使用 getStaticProps 函数 getStaticProps 返回一个道具,当我在我的主要组件中记录道具时,我收到了 undifined我的控制台。 我想知道使用 getStaticProps 是否错误,我必须在页面中使用该功能。 我是下一个 js 的新手,如果有人能帮助我,我将不胜感激。

这是我的主要组件

import React from 'react';
import {IMain} from "../../../../interfaces/components/IMenu/IMain";

const Main:React.FC<IMain> = (props) => {
    console.log(props);
    return (
        <div>
        </div>
    );
};


export async function getServerSideProps() {
    return {
        props: {
            data: 'gg'
        }
    };
}

export default Main;

这是我的 index.js 页面

import Text from "./../components/ui/Text/Text";
import Button from "../components/ui/Button/Button";
import Main from "../components/Menu/Desktop/Main/Main";

const Home = () => {
  return <Main/>;
};




export default Home;

【问题讨论】:

标签: javascript reactjs typescript next.js


【解决方案1】:

getStaticProps 只能从页面导出。不能从非页面文件中导出。如果将getStaticProps添加为页面组件的属性,将无法使用。

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

【讨论】:

猜你喜欢
  • 2021-12-03
  • 2020-11-05
  • 2021-10-03
  • 2021-06-06
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 1970-01-01
相关资源
最近更新 更多