0
0
NextJSframework~20 mins

Layout vs template difference in NextJS - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Layout & Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main difference between a layout and a template in Next.js?
In Next.js, both layouts and templates help organize pages. Which statement best describes their difference?
ALayouts wrap multiple pages and keep state between navigations, while templates are used to render individual pages without preserving state.
BTemplates wrap multiple pages and keep state between navigations, while layouts are used to render individual pages without preserving state.
CLayouts and templates are exactly the same and can be used interchangeably in Next.js.
DTemplates are only used for server components, while layouts are only used for client components.
Attempts:
2 left
💡 Hint
Think about which one keeps state when you move between pages.
component_behavior
intermediate
2:00remaining
What happens to state inside a layout when navigating between pages using templates in Next.js?
Consider a layout component with a counter state. If you navigate between pages rendered by different templates inside this layout, what happens to the counter state?
AThe counter state resets to initial value on every page navigation.
BThe counter state is shared only if pages use the same template, otherwise it resets.
CThe counter state persists and keeps its value across page navigations.
DThe counter state is lost only if you refresh the browser, but not on navigation.
Attempts:
2 left
💡 Hint
Think about where the state is stored and if the layout stays mounted.
📝 Syntax
advanced
2:00remaining
Which Next.js code snippet correctly defines a layout that wraps pages and preserves state?
Select the code that correctly implements a layout component in Next.js 14+ that wraps children and preserves state.
A
export default function Layout({ children }) {
  const [count, setCount] = React.useState(0);
  return <div>{children}<button onClick={() => setCount(count + 1)}>{count}</button></div>;
}
B
export default function Template({ children }) {
  const [count, setCount] = React.useState(0);
  return <div>{children}<button onClick={() => setCount(count + 1)}>{count}</button></div>;
}
C
export default function Layout({ children }) {
  const count = 0;
  return <div>{children}<button onClick={() => count++}>{count}</button></div>;
}
D
export default function Layout() {
  const [count, setCount] = React.useState(0);
  return <div><button onClick={() => setCount(count + 1)}>{count}</button></div>;
}
Attempts:
2 left
💡 Hint
Layouts receive children as props and can hold state with hooks.
🔧 Debug
advanced
2:00remaining
Why does state reset when navigating between pages in this Next.js layout?
Given this layout code, why does the counter reset to 0 every time you navigate to a new page? ```jsx export default function Layout({ children }) { const [count, setCount] = React.useState(0); return
{children}
; } ``` Assume pages are rendered with templates inside this layout.
AThe state hook is used incorrectly and should be useSignal instead of useState.
BThe button's onClick handler is not updating state properly, causing reset.
CThe children prop is missing, so the layout remounts on every navigation.
DThe layout is inside the app directory but not in the correct folder, so Next.js remounts it on navigation.
Attempts:
2 left
💡 Hint
Check where the layout file is placed in the Next.js app folder structure.
lifecycle
expert
3:00remaining
How does Next.js handle layout and template rendering order during navigation?
In Next.js 14+, when navigating between pages that share the same layout but different templates, what is the correct rendering lifecycle order?
ABoth layout and template components unmount and remount on every page navigation.
BThe layout component stays mounted, only the template component unmounts and remounts for each page.
CThe template component stays mounted, and the layout component unmounts and remounts on navigation.
DNeither layout nor template components unmount; Next.js reuses all components without remounting.
Attempts:
2 left
💡 Hint
Think about which part preserves state and which part changes per page.