Trying to use getStaticProps in a layout.js
I am working on an application and I would like to use getStaticProps in a layout .js file of my routing. I keep getting an error that this is not supported but in the documentation they say that it only works in pages but I get the same error if I try to use it in page.js. The project is Next v 13. How can a fix this or are there any alernatives?
15 Replies
Why is
Page
a client component?
If it wasn't, you can make the Page
component async
& do the data fetching in the component.I think because a component from page that needs it to be client
So, you get an error if you remove
"use client"
?
I was thinking you only needed the client directive on the actual component that has to be rendered on the client.Yes I do
atleast I think its because of a component
Can you paste in the error? (I'm still learning the app router & would like to flesh out my understanding a bit.)
this is the whole error
Do you get an error when you remove
"use client"
?yes I get this
Do you have
"use client"
in the child components that use useState
?
From the docs:
However, "use client" doesn't need to be defined in every component that needs to be rendered on the client. Once you define the boundary, all child components and modules imported into it are considered part of the client bundle.So removing
"use client"
from Page
would make the children server components if they don't have the "use client"
boundary. One of them could be throwing the error.
Is there a file / line number in the error?
its needed in the component for the usePathName I think
Yes, but you are wanting to load data in the
Page
component, right?
If you can push the "use client"
down to the child components, you can remove it from Page
, make Page
async
, and do your data loading directly in the Page
component.So you mean I should only place "use client" in the component its needed?
Yeah. Specifically, don't put it in the
Page
component.
If this is confusing, we could hop on #voice-chat & I'll step you through it.it is a little
i'm in voice
Thanks alot!
SOLVED: We made sure the the components that needed to have '"use client" had it so it could be removed in page.js. Like this the fetch could just be done inside the page component and on the server.