I started a project with next js and typescript. I have a main component that I call it in the index.js page I use the getStaticProps function in the main component getStaticProps returns a prop object and when I log this prop in my main component I received undefined in my console. I want to know using the getStaticProps in the component is wrong and I have just to use that function in pages or not. I am a newbie in next js and I would be very grateful if anyone could help me.
this is my main component
JavaScript
x
21
21
1
import React from 'react';
2
import {IMain} from "../../../../interfaces/components/IMenu/IMain";
3
4
const Main:React.FC<IMain> = (props) => {
5
console.log(props);
6
return (
7
<div>
8
</div>
9
);
10
};
11
12
13
export async function getServerSideProps() {
14
return {
15
props: {
16
data: 'ggwp'
17
}
18
};
19
}
20
21
export default Main;
and this is my index.js page
JavaScript
1
12
12
1
import Text from "./../components/ui/Text/Text";
2
import Button from "../components/ui/Button/Button";
3
import Main from "../components/Menu/Desktop/Main/Main";
4
5
const Home = () => {
6
return <Main/>;
7
};
8
9
10
11
12
export default Home;
Advertisement
Answer
getStaticProps can only be exported from a page. You can’t export it from non-page files.It will not work if you add getStaticProps as a property of the page component.