Hint: Use object with top and behavior keys for smooth scroll [OK]
Common Mistakes:
Passing parameters as separate arguments instead of object
Using incorrect keys like 'smooth' instead of 'behavior'
Misplacing the 'smooth' value
3. Consider this Next.js code snippet inside a component: <pre>useEffect(() => {
router.events.on('routeChangeComplete', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}, []); What will happen when the route changes?
medium
A. The page will smoothly scroll to the top after navigation.
B. Nothing will happen because the event listener is not removed.
C. The page will jump instantly to the top.
D. The page will reload after scrolling.
Solution
Step 1: Understand event listener on routeChangeComplete
The code listens for the route change completion event and triggers scrolling.
Step 2: Analyze scrollTo call
It calls window.scrollTo with behavior: 'smooth', so scrolling is smooth to top.
Final Answer:
The page will smoothly scroll to the top after navigation. -> Option A
Quick Check:
routeChangeComplete + smooth scroll = smooth scroll top [OK]
Hint: routeChangeComplete triggers smooth scroll to top [OK]
Common Mistakes:
Assuming instant jump instead of smooth scroll
Thinking event listener removal is required to trigger scroll
Confusing scroll with page reload
4. This Next.js code tries to scroll smoothly on route change: <pre>useEffect(() => {
router.events.on('routeChangeComplete', () => {
window.scrollTo({ top: 0, behavior: 'smooth' });
});
}, []); But after navigating multiple times, scrolling stops working. What is the likely problem?
medium
A. The event listener is added multiple times without cleanup, causing issues.
B. The scrollTo syntax is incorrect and causes errors.
C. Next.js does not support scroll control on route changes.
D. The behavior 'smooth' is not supported in modern browsers.
Solution
Step 1: Identify event listener management
The code adds an event listener on mount but never removes it, so multiple listeners accumulate.
Step 2: Understand effect of multiple listeners
Multiple listeners cause unexpected behavior or conflicts, stopping scroll from working properly.
Final Answer:
The event listener is added multiple times without cleanup, causing issues. -> Option A
Quick Check:
Missing cleanup causes multiple listeners = problem [OK]
Hint: Always remove event listeners in useEffect cleanup [OK]
Common Mistakes:
Ignoring cleanup of event listeners
Blaming scrollTo syntax which is correct
Assuming browser support is the issue
5. You want to scroll smoothly to a specific section with id 'contact' after navigating to a new page in Next.js. Which approach correctly achieves this?
hard
A. Use window.scrollTo(0, 0); after navigation and then call scrollIntoView without behavior option.
B. Use window.scrollTo({ top: document.getElementById('contact').offsetTop, behavior: 'auto' }); inside routeChangeStart event.
C. Use router.events.on('routeChangeComplete', () => { document.getElementById('contact').scrollIntoView({ behavior: 'smooth' }); });
D. Use router.push('/contact'); without any scroll control; rely on default browser behavior.
Solution
Step 1: Choose correct event and method
Use routeChangeComplete to ensure page is loaded, then scroll to element with smooth behavior.
Step 2: Verify scrollIntoView usage
scrollIntoView({ behavior: 'smooth' }) scrolls smoothly to the element with id 'contact'.
Final Answer:
Use router.events.on('routeChangeComplete', () => { document.getElementById('contact').scrollIntoView({ behavior: 'smooth' }); }); -> Option C