#layout children
1 messages · Page 1 of 1 (latest)
Hi @short dome. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
In a Next.js layout, the children prop is not limited to just the same level's page.tsx. The children prop in a layout component represents all the route segments the layout is wrapping, which can include:
- The component of a child layout (if it exists)
- A page component
- Special files like Loading or Error components when applicable
According to the Next.js documentation:
Layout components should accept and use a
childrenprop. During rendering,childrenwill be populated with the route segments the layout is wrapping. These will primarily be the component of a child Layout (if it exists) or Page, but could also be other special files like Loading or Error when applicable.
Next.js Documentation
This means that a layout wraps not just the page at its level, but also any nested layouts and their respective pages. Layouts are nested, so each layout wraps the layouts and pages below it in the route hierarchy.
For example, if you have:
- app/layout.tsx (root layout)
- app/dashboard/layout.tsx
- app/dashboard/page.tsx
- app/dashboard/settings/page.tsx
The children in app/layout.tsx would include app/dashboard/layout.tsx, which in turn has its own children that would be either app/dashboard/page.tsx or app/dashboard/settings/page.tsx depending on the current route.
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@short dome Kapa.ai is still learning and improving, please let me know how I did by reacting below.
@unkempt pebble
loading.js only works for page.tsx right?
Let me check the knowledge base for you again. Give me one moment! :books:
No, loading.js doesn't only work for page.tsx. The loading.js file creates a loading state for an entire route segment, which includes all components within that segment - not just the page component.
According to the Next.js documentation:
The special file
loading.jshelps you create meaningful Loading UI with React Suspense. With this convention, you can show an instant loading state from the server while the content of a route segment loads.
Loading UI and Streaming
When you add a loading.js file to a folder, it will:
- Be nested inside the
layout.jsof the same folder - Automatically wrap the
page.jsfile and any children below it in a<Suspense>boundary - Show while the content of that route segment (and its children) is loading
However, there are some important behaviors to understand:
-
When navigating between nested routes, the loading behavior can be inconsistent. For example, if you navigate from a parent route to a child route, the parent's loading state might not be shown again because React won't re-trigger Suspense boundaries that have already been resolved unless the whole subtree is remounted.
Github Issue -
For more granular loading states within a page, you should use React's
<Suspense>component directly rather than relying on theloading.jsconvention.
The loading.js convention is specifically recommended for route segments (layouts and pages) as Next.js optimizes this functionality.