Performance: Why layouts avoid redundant rendering
This concept affects page load speed and interaction responsiveness by reducing unnecessary re-rendering of shared UI parts.
function Layout({ children }) { return ( <> <header>Site Header</header> <main>{children}</main> <footer>Site Footer</footer> </> ) } export default function Page() { return <Layout>Page Content</Layout> }
export default function Page() { return ( <> <header>Site Header</header> <main>Page Content</main> <footer>Site Footer</footer> </> ) }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No layout, repeated UI in pages | High (header/footer recreated each page) | Multiple reflows per navigation | High paint cost due to full UI repaint | [X] Bad |
| Shared layout wrapping pages | Low (header/footer mounted once) | Single reflow on initial load | Lower paint cost, only main content repainted | [OK] Good |
children prop to wrap page content.{children} to render nested pages properly.-- layout.js --
export default function Layout({ children }) {
console.log('Layout rendered');
return {children};
}
-- pageA.js --
export default function PageA() {
console.log('Page A rendered');
return Page A
;
}
-- pageB.js --
export default function PageB() {
console.log('Page B rendered');
return Page B
;
}/dashboard. How should you structure your Next.js app router folders and layouts to avoid redundant rendering?layout.js inside dashboard/ folder wraps all nested pages, keeping sidebar mounted.dashboard/layout.js with the sidebar and place nested pages inside dashboard/ folder. -> Option DPass the test and this pattern moves unknown