Unable to set a default layout in React which uses usePage() #1216
-
I'm trying to create a default layout in React. Here is the code in createInertiaApp({
resolve: (name) => {
return import(`./Pages/${name}`).then(({ default: page }) => {
if (page.layout === undefined) {
page.layout = AppLayout
}
return page
})
},
setup({ el, App, props }) {
const root = createRoot(el)
root.render(<App {...props} />)
},
}) And this is my layout: import React from 'react'
import { ISharedData } from '../types'
import { usePage } from '@inertiajs/inertia-react'
export default function AppLayout(children) {
const {
props: { user }
} = usePage<ISharedData>()
return (
<div>
<div className="w-full p-4 bg-blue-500">i am a navbar</div>
{children}
</div>
)
} With this, I get a bunch of errors in the console and a blank page:
The problem doesn't seem to be the layout - I can manually wrap the page in I would be grateful for any suggestions to fix this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
The solution:
Hopefully this will help someone in the future with the same problem. |
Beta Was this translation helpful? Give feedback.
-
I have a question with this. Let's imagine i have this URL setup /projects
/projects/:id And I want to display the last 5 visited projects in my Layout > Sidebar import { ReactNode } from 'react'
import AppLayout from '@/Layouts/AppLayout'
import { usePage } from '@inertiajs/react'
import { PageProps } from '@/types'
function ProjectPage() {
return (
<>
<div className='grid auto-rows-min gap-4 md:grid-cols-3'>
<div className='bg-muted/50 aspect-video rounded-xl' />
<div className='bg-muted/50 aspect-video rounded-xl' />
<div className='bg-muted/50 aspect-video rounded-xl' />
</div>
<div className='bg-muted/50 min-h-[100vh] flex-1 rounded-xl md:min-h-min' />
</>
)
}
ProjectPage.layout = function Layout(children: ReactNode) {
const { project } = usePage<PageProps>().props
return (
<AppLayout
title={project.name}
children={children}
breadcrumbs={[{ label: 'Dashboard' }, { label: project.name, href: `/projects/${project.id}`}]}
/>
)
}
export default ProjectPage How could I do something like that? |
Beta Was this translation helpful? Give feedback.
The solution:
usePage()
to a new component and return that component from the layout.Hopefully this will help someone in the future with the same problem.