In /app directory, is there any way to pass data to the layout even statically? #44506
-
I've run into an issue where the designed layout I'm trying to use features a page title as part of the Because of the strict wall that
I'll leave a comment on the app directory RFC with what I was expecting, but in general I'm wondering if there's something I've missed about a replacement for |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I'm having a similar issue and so far my solution is to call This however has the downside that client state introduced in the time the So it's something like: // layout.tsx
const getData (id) => { return pageTitle }
const Layout = async (props: { children: React.ReactNode, params: { pageId: string } }) => {
const pageTitle = await getData(props.params.id)
return (<><h1>{pageTitle}</h1><div>{children}</div></>)
} // page.tsx
const Page = async (props: { params: { pageId: string } }) => {
return (<><div><ClientComponent /></>)
} // components/ClientComponent.tsx
'use client'
import { useRouter } from 'next/navigation'
const ClientComponent = () => {
const router = useRouter()
useEffect(() => { router.refresh() }, [router])
return (<>Content</>)
} |
Beta Was this translation helpful? Give feedback.
-
Any updates? |
Beta Was this translation helpful? Give feedback.
-
Next 13 and 14 update with |
Beta Was this translation helpful? Give feedback.
-
You shouldn't need to pass data – instead, you would call the same function multiple times and use React's You'll notice this pattern in a few places throughout Next.js. For example, calling the same API in the default exported component for a page as well as |
Beta Was this translation helpful? Give feedback.
You shouldn't need to pass data – instead, you would call the same function multiple times and use React's
cache()
function.You'll notice this pattern in a few places throughout Next.js. For example, calling the same API in the default exported component for a page as well as
generateMetadata
orgenerateStaticParams
.