0
0
NextJSframework~20 mins

Shallow routing concept in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shallow Routing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when shallow routing is used in Next.js?
Consider a Next.js page that uses shallow routing to change the URL query without running getServerSideProps again. What is the expected behavior?
NextJS
import { useRouter } from 'next/router';

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

  function changeQuery() {
    router.push({ pathname: '/page', query: { tab: '2' } }, undefined, { shallow: true });
  }

  return (
    <>
      <p>Current tab: {router.query.tab || '1'}</p>
      <button onClick={changeQuery}>Change Tab</button>
    </>
  );
}
AThe URL does not update, but the component re-renders with new query values.
BThe URL updates but the component does not re-render.
CThe URL updates and the page fully reloads, calling getServerSideProps again.
DThe URL updates and the component re-renders without calling getServerSideProps or getStaticProps again.
Attempts:
2 left
💡 Hint
Shallow routing updates the URL without a full page reload or data fetching methods running again.
📝 Syntax
intermediate
2:00remaining
Which code snippet correctly uses shallow routing in Next.js?
Select the code snippet that correctly performs shallow routing to update the URL query without a full page reload.
Arouter.push('/page?tab=2', undefined, { shallow: true });
Brouter.replace('/page?tab=2', undefined, { shallow: false });
Crouter.push('/page?tab=2', undefined, { shallow: false });
Drouter.replace('/page?tab=2', undefined, { shallow: true });
Attempts:
2 left
💡 Hint
Shallow routing requires the shallow option set to true and usually uses router.push to add a new history entry.
🔧 Debug
advanced
2:30remaining
Why does shallow routing not update the component state as expected?
A developer uses shallow routing to update the URL query, but the component does not reflect the new query value. What is the most likely cause?
NextJS
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';

export default function Page() {
  const router = useRouter();
  const [tab, setTab] = useState(router.query.tab || '1');

  useEffect(() => {
    setTab(router.query.tab || '1');
  }, []);

  function changeTab() {
    router.push({ pathname: '/page', query: { tab: '2' } }, undefined, { shallow: true });
  }

  return (
    <>
      <p>Current tab: {tab}</p>
      <button onClick={changeTab}>Change Tab</button>
    </>
  );
}
AShallow routing does not update the URL query, so tab remains the same.
BThe useEffect has an empty dependency array, so it does not update tab when router.query changes.
CThe router.push call is missing the shallow option, causing a full reload.
DThe initial state uses router.query.tab which is undefined, causing an error.
Attempts:
2 left
💡 Hint
Check how useEffect dependencies affect state updates when router.query changes.
🧠 Conceptual
advanced
2:00remaining
What is a key limitation of shallow routing in Next.js?
Which of the following is a limitation when using shallow routing in Next.js?
AIt does not trigger getServerSideProps or getStaticProps, so data fetching is not re-run automatically.
BIt causes a full page reload, losing client-side state.
CIt cannot update the URL query parameters.
DIt disables client-side navigation and forces server navigation.
Attempts:
2 left
💡 Hint
Think about what shallow routing skips compared to normal routing.
state_output
expert
2:30remaining
What is the value of router.query.tab after shallow routing?
Given the following code, what will be logged to the console after clicking the button?
NextJS
import { useRouter } from 'next/router';
import { useEffect } from 'react';

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

  useEffect(() => {
    console.log('Tab query:', router.query.tab);
  }, [router.query.tab]);

  function changeTab() {
    router.push({ pathname: '/page', query: { tab: '3' } }, undefined, { shallow: true });
  }

  return <button onClick={changeTab}>Change Tab</button>;
}
A'2'
Bundefined
C'3'
Dnull
Attempts:
2 left
💡 Hint
Shallow routing updates the URL query and router.query state immediately.