//pages
function MyApp({ Component, pageProps: {session, ...pageProps},example }: AppProps & AppOwnProps) {
return (
<SessionProvider session={session}>
<Layout data={example}>
<Component {...pageProps} />
</Layout>
</SessionProvider>
)
}
//layout
<Fragment>
<Header productData={props.data}/>
<Navbar />
<main className={styles.main}>{props.children}</main>
<Footer />
</Fragment>
//component
const Header... {
<Sidebar data={props.productData}>
}
there is a nextjs in this build. I should do something like getstaticprops . I establish the mongodb connection and pull my product categories and show in Sidebar. getstaticprops only works inside page. my sidebar is a component.
I pull the products using getinitialprops in the app and transmit them with props. There is no problem until here, but it does not load other pages again. in page transitions, it only switches to the pages that are processed with the database.
What should be the best way to take my products and transfer them to the sidebar?
in _app.tsx ?
MyApp.getInitialProps = async (
context: AppContext
): Promise<AppOwnProps & AppInitialProps> => {
const ctx = await App.getInitialProps(context);
let res = await fetch(`${process.env.NEXTAUTH_URL}/api/prodController`,{method:'GET',headers: {
"Content-Type": "application/json",
},});
const data =await res.json()
return { ...ctx, example: JSON.parse(JSON.stringify(data)) };
};
but not reload other page.
it won't work in the component when done like this
export const getStaticProps: GetStaticProps = async (context) => {
// get data from API
let res = await fetch(`${process.env.NEXTAUTH_URL}/api/prodController`,{method:'GET'});
const data =await res.json()
// return props
return {
props: { posts: data },
}