Revalidation after mutation helps keep your website data fresh by updating the page after you change something.
Revalidation after mutation in NextJS
'use client'; import { useRouter } from 'next/navigation'; export default function Component() { const router = useRouter(); // After mutation router.refresh(); }
router.refresh() tells Next.js to reload the current page data.
This works well with Next.js App Router and client components.
'use client'; import { useRouter } from 'next/navigation'; export default function Page() { const router = useRouter(); async function handleUpdate() { await fetch('/api/update', { method: 'POST' }); router.refresh(); } return <button onClick={handleUpdate}>Update Data</button>; }
'use client'; import { useRouter } from 'next/navigation'; export default function DeleteButton({ id }) { const router = useRouter(); async function handleDelete() { await fetch(`/api/delete/${id}`, { method: 'DELETE' }); router.refresh(); } return <button onClick={handleDelete}>Delete Item</button>; }
This form sends a new comment to the server. After sending, it refreshes the page to show the new comment without needing a manual reload.
'use client'; import { useRouter } from 'next/navigation'; export default function CommentForm() { const router = useRouter(); async function submitComment(event) { event.preventDefault(); const formData = new FormData(event.target); await fetch('/api/comments', { method: 'POST', body: JSON.stringify({ text: formData.get('comment') }), headers: { 'Content-Type': 'application/json' }, }); router.refresh(); } return ( <form onSubmit={submitComment} aria-label="Add comment form"> <label htmlFor="comment">Comment:</label> <input id="comment" name="comment" required /> <button type="submit">Add Comment</button> </form> ); }
Use router.refresh() only after the mutation completes successfully.
This method reloads the current route's data but keeps the UI state intact.
It works best with Next.js App Router and client components.
Revalidation after mutation updates page data after changes.
Use router.refresh() to reload data in Next.js App Router.
This keeps your UI showing fresh, accurate information automatically.
Practice
Solution
Step 1: Understand mutation effect
When data changes (mutation), the UI might show old data if not updated.Step 2: Purpose of revalidation
Revalidation reloads or refreshes data so the UI reflects the latest changes.Final Answer:
To update the page data after a change so the UI shows fresh information -> Option AQuick Check:
Revalidation updates data = A [OK]
- Thinking revalidation stops data saving
- Confusing revalidation with full page reload
- Believing it disables user actions
Solution
Step 1: Identify method for revalidation
Next.js App Router providesrouter.refresh()to reload data without full page reload.Step 2: Differentiate from other methods
router.reload()reloads whole page,router.push()androuter.replace()change routes.Final Answer:
router.refresh() -> Option DQuick Check:
Revalidation uses router.refresh() = A [OK]
- Using router.reload() which reloads full page
- Confusing route navigation methods with revalidation
- Trying to use router.replace() for data refresh
import { useRouter } from 'next/navigation';
export default function UpdateButton() {
const router = useRouter();
async function handleClick() {
await fetch('/api/update', { method: 'POST' });
router.refresh();
}
return Update;
}What happens when the button is clicked?
Solution
Step 1: Analyze handleClick function
It calls the API with POST, then callsrouter.refresh()to reload data.Step 2: Understand router.refresh() effect
This refreshes the current route's data without a full page reload or navigation.Final Answer:
The API is called, then the page data refreshes without full reload -> Option AQuick Check:
API call + router.refresh() updates data = C [OK]
- Thinking router.refresh() reloads entire page
- Assuming router.refresh() navigates to API route
- Believing nothing happens due to syntax error
import { useRouter } from 'next/navigation';
export default function DeleteButton() {
const router = useRouter;
async function handleDelete() {
await fetch('/api/delete', { method: 'DELETE' });
router.refresh();
}
return Delete;
}Solution
Step 1: Check router initialization
Code usesconst router = useRouter;missing parentheses, so router is a function reference, not the router object.Step 2: Effect on router.refresh()
Callingrouter.refresh()fails because router is not an object but a function reference.Final Answer:
router is not called as a function, so router.refresh() fails -> Option CQuick Check:
useRouter missing () causes error = D [OK]
- Forgetting parentheses on useRouter()
- Thinking DELETE method is invalid
- Confusing onClick with onSubmit
Solution
Step 1: Understand goal
After mutation, the UI must show updated list without full reload or navigation.Step 2: Evaluate options
router.push('/current-page')navigates and reloads route,window.location.reload()reloads full page, and doing nothing won't update data.Step 3: Correct method
router.refresh()triggers Next.js to revalidate data and update UI efficiently.Final Answer:
Call router.refresh() after mutation to revalidate and update data -> Option BQuick Check:
Use router.refresh() to update data after mutation = B [OK]
- Using router.push() causing unnecessary navigation
- Reloading full page losing state
- Assuming UI updates automatically without refresh
Ready to prove it?
Pass the test and this pattern moves unknown
