0
0
NextJSframework~20 mins

URL state with searchParams in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
URL State Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does the component update URL searchParams?

Consider a Next.js component that updates the URL's searchParams when a user selects a filter. What happens when the user selects a new filter?

NextJS
import { useSearchParams, useRouter } from 'next/navigation';
import { useState } from 'react';

export default function FilterComponent() {
  const searchParams = useSearchParams();
  const router = useRouter();
  const [filter, setFilter] = useState(searchParams.get('filter') || 'all');

  function onChangeFilter(newFilter) {
    setFilter(newFilter);
    const params = new URLSearchParams(searchParams.toString());
    params.set('filter', newFilter);
    router.replace(`?${params.toString()}`);
  }

  return (
    <select value={filter} onChange={e => onChangeFilter(e.target.value)} aria-label="Select filter">
      <option value="all">All</option>
      <option value="active">Active</option>
      <option value="completed">Completed</option>
    </select>
  );
}
AThe URL's searchParams update only after a manual page refresh.
BThe URL's searchParams update but cause a full page reload, losing component state.
CThe component state updates but the URL's searchParams remain unchanged.
DThe URL's searchParams update with the new filter value without a page reload, and the component state updates accordingly.
Attempts:
2 left
💡 Hint

Think about how router.replace works in Next.js App Router.

📝 Syntax
intermediate
1:30remaining
Identify the correct way to read searchParams in a Next.js client component

Which code snippet correctly reads the 'page' parameter from the URL searchParams inside a Next.js client component?

Aconst page = new URL(window.location).searchParams.page;
Bconst searchParams = useSearchParams(); const page = searchParams.get('page');
Cconst page = useRouter().query.page;
Dconst page = window.location.search.get('page');
Attempts:
2 left
💡 Hint

Remember that useSearchParams returns a URLSearchParams object.

🔧 Debug
advanced
2:30remaining
Why does the URL not update when calling router.replace with new searchParams?

Given this code snippet inside a Next.js client component, why does the URL not update as expected?

const searchParams = useSearchParams();
const router = useRouter();

function updateCategory(cat) {
  const params = searchParams;
  params.set('category', cat);
  router.replace(`?${params.toString()}`);
}
ABecause searchParams is immutable and calling set on it does not change the original object.
BBecause router.replace requires a full URL, not just searchParams string.
CBecause useRouter cannot be used inside client components.
DBecause the function updateCategory is missing an async keyword.
Attempts:
2 left
💡 Hint

Check if searchParams can be modified directly.

state_output
advanced
2:00remaining
What is the value of 'filter' after URL changes externally?

In a Next.js client component, the state variable filter is initialized from useSearchParams().get('filter'). If the user manually changes the URL's filter parameter in the browser address bar, what happens to the filter state variable?

NextJS
import { useSearchParams } from 'next/navigation';
import { useState } from 'react';

export default function Filter() {
  const searchParams = useSearchParams();
  const [filter, setFilter] = useState(searchParams.get('filter') || 'all');

  return <div>Current filter: {filter}</div>;
}
AThe filter state remains unchanged until the component re-mounts or reloads.
BThe filter state updates immediately when the URL changes externally.
CThe filter state resets to 'all' automatically on URL change.
DThe component throws an error because state and URL are out of sync.
Attempts:
2 left
💡 Hint

Think about how React state works with initial values.

🧠 Conceptual
expert
3:00remaining
Why use URL searchParams for state in Next.js App Router?

What is the main advantage of syncing UI state with URL searchParams in a Next.js App Router application?

AIt prevents any client-side state updates, forcing all changes to reload the page.
BIt automatically improves server-side rendering performance by caching searchParams.
CIt allows users to share and bookmark the current UI state via the URL, enabling better navigation and persistence.
DIt disables browser back and forward buttons to avoid inconsistent UI states.
Attempts:
2 left
💡 Hint

Consider how URLs help users share or return to specific views.