0
0
NextJSframework~20 mins

Scroll behavior control in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scroll Mastery in Next.js
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when this Next.js component uses useEffect to scroll?
Consider this Next.js functional component that scrolls the window to the top when it mounts. What will the user see after the component loads?
NextJS
import { useEffect } from 'react';

export default function ScrollTop() {
  useEffect(() => {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  }, []);

  return <div style={{ height: '200vh' }}>Scroll and reload to test</div>;
}
AThe page jumps instantly to the top without animation.
BThe page smoothly scrolls to the top on load.
CNothing happens; the page stays where it was.
DThe page scrolls to the bottom smoothly.
Attempts:
2 left
💡 Hint
Think about what window.scrollTo with behavior 'smooth' does inside useEffect on mount.
📝 Syntax
intermediate
2:00remaining
Which option correctly disables scroll restoration in Next.js client-side navigation?
Next.js uses the browser's scroll restoration by default. Which code snippet correctly disables it to control scroll manually?
NextJS
import { useEffect } from 'react';
import { useRouter } from 'next/router';

export default function DisableScrollRestoration() {
  const router = useRouter();

  useEffect(() => {
    // Your code here
  }, [router]);

  return <div>Content</div>;
}
Awindow.history.scrollRestoration = 'manual';
Bwindow.scrollRestoration = 'manual';
Chistory.scrollRestoration = 'manual';
Dwindow.history.scrollRestore = 'manual';
Attempts:
2 left
💡 Hint
Check the correct property on window.history to control scroll restoration.
🔧 Debug
advanced
2:00remaining
Why does this Next.js scroll control code fail to scroll smoothly on route change?
This code tries to scroll smoothly to top on route change but the scroll is instant. What is the cause?
NextJS
import { useEffect } from 'react';
import { useRouter } from 'next/router';

export default function ScrollOnRouteChange() {
  const router = useRouter();

  useEffect(() => {
    window.scrollTo({ top: 0, behavior: 'smooth' });
  }, [router.pathname]);

  return <div style={{ height: '200vh' }}>Page content</div>;
}
AThe component does not render enough content to scroll.
Bwindow.scrollTo does not support smooth behavior in modern browsers.
CNext.js scroll restoration conflicts and resets scroll instantly after useEffect runs.
DThe useEffect dependency is wrong; it should be router instead of router.pathname.
Attempts:
2 left
💡 Hint
Next.js automatically restores scroll position on navigation unless disabled.
🧠 Conceptual
advanced
2:00remaining
What is the best way to implement smooth scroll to a section on the same page in Next.js?
You want to smoothly scroll to a section with id 'features' when clicking a button. Which approach works best in Next.js?
AUse <a href="#features"> with CSS scroll-behavior: smooth on html element.
BUse router.push('/page#features') and rely on Next.js scroll restoration.
CUse window.scrollTo({ top: document.getElementById('features').offsetTop }) without behavior.
DUse window.location.hash = '#features' without smooth behavior.
Attempts:
2 left
💡 Hint
Native browser smooth scrolling with CSS is simple and reliable.
state_output
expert
3:00remaining
What is the scroll position after this Next.js component runs?
This component disables automatic scroll restoration and manually scrolls to 100px from top on route change. What is the final scroll position after navigation?
NextJS
import { useEffect } from 'react';
import { useRouter } from 'next/router';

export default function ManualScroll() {
  const router = useRouter();

  useEffect(() => {
    if ('scrollRestoration' in window.history) {
      window.history.scrollRestoration = 'manual';
    }

    const handleRouteChange = () => {
      window.scrollTo({ top: 100, behavior: 'auto' });
    };

    router.events.on('routeChangeComplete', handleRouteChange);

    return () => {
      router.events.off('routeChangeComplete', handleRouteChange);
    };
  }, [router.events]);

  return <div style={{ height: '300vh' }}>Long content</div>;
}
AThe page stays at the previous scroll position after navigation.
BThe page scrolls smoothly to 100 pixels from the top after navigation.
CThe page scrolls instantly to the top (0 pixels) after navigation.
DThe page scrolls instantly to 100 pixels from the top after navigation.
Attempts:
2 left
💡 Hint
Check the scrollRestoration setting and the scrollTo behavior used.