Think about what happens when you move between pages that share the same layout.
Layouts in Next.js keep shared UI elements like headers or sidebars persistent. This means when you navigate between pages inside the layout, only the nested content changes. This avoids re-rendering the entire UI, making navigation faster and smoother.
import React from 'react'; export default function Layout({ children }) { const [count, setCount] = React.useState(0); return ( <> <button onClick={() => setCount(count + 1)}>Count: {count}</button> {children} </> ); }
Think about whether the layout component unmounts or stays mounted during navigation.
Because layouts persist across page navigations in Next.js, their state also persists. The layout component does not unmount, so the counter state keeps its value as you move between pages inside the layout.
Look for a layout that wraps children with shared UI components.
Option C correctly wraps children with shared components like Header and Footer. This layout persists and avoids re-rendering these shared parts on navigation. Option C lacks shared UI, B does not accept children, and D has unreachable code after return.
import React from 'react'; export default function Layout({ children }) { const [time, setTime] = React.useState(Date.now()); React.useEffect(() => { const id = setInterval(() => setTime(Date.now()), 1000); return () => clearInterval(id); }, []); return ( <> <Header currentTime={time} /> {children} </> ); }
Consider what causes React components to re-render.
The Header component receives a prop 'currentTime' that updates every second. This changing prop causes Header to re-render frequently, even if the page navigation does not change. The layout itself persists, but the prop change triggers re-render.
import React from 'react'; export default function Layout({ children }) { const [count, setCount] = React.useState(0); return ( <> <button onClick={() => setCount(c => c + 1)}>Count: {count}</button> {children} </> ); } // User clicks button twice on Page A, then navigates to Page B inside this layout.
Remember how layout state behaves across page navigations.
The layout component stays mounted during navigation, so its state persists. After clicking twice, count is 2. Navigating to Page B does not reset the state, so the count remains 2.