Layouts & TemplatesIntermediate
1Question 1 of 1
typescriptWhat is the difference between Layouts and Templates in the Next.js App Router?
Explanation & Concept
• Layouts (`layout.tsx`): Preserve state across navigation, do not re-render, and do not re-mount child DOM nodes. • Templates (`template.tsx`): Similar to layouts, but create a brand new instance for each child route navigation. State is reset, and `useEffect` hooks re-run. Ideal for enter/exit animations and page view tracking.
Code Example
typescript
// app/template.tsx
"use client";
import { useEffect } from "react";
export default function Template({ children }: { children: React.ReactNode }) {
useEffect(() => {
// Runs on EVERY page navigation!
console.log("Track page view analytics");
}, []);
return <div className="animate-fade-in">{children}</div>;
}How It Works & Key Takeaways
Use `template.tsx` when you need animations or analytics that must fire on every page transition.
Mock Test · 1 Questions · Optional
Ready for Metadata API?
Take a quick test to evaluate yourself, or move straight to the next module.
Advanced1 questions