【发布时间】: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;
【问题讨论】:
-
在示例中,您使用的是 getServerSideProps 而不是 getStaticProps 顺便说一句。我也尝试了这段代码并且它有效,我不得不刷新页面。热加载器没有立即将其拾取。
标签: javascript reactjs typescript next.js