Unable to set a default layout in React which uses usePage() #1216
Answered
by
finestgecko
finestgecko
asked this question in
Help (React)
-
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.
Answered by
finestgecko
Jul 5, 2022
Replies: 1 comment
-
The solution:
Hopefully this will help someone in the future with the same problem. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
finestgecko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.