EasyState Management📖 Theory Question

When should you use URL state instead of React state?

💡

Hint

If the state should survive a page refresh, be shareable via a link, or be bookmarkable — put it in the URL

Full Answer

URL state (query params, path params) is the right choice whenever state should be:

  • Shareable — a user can send a link and the recipient sees the same view. Filters, pagination, search terms, selected tabs.
  • Bookmarkable — browser history works correctly; back button returns to previous filter state.
  • Server-renderable — the server can pre-render the correct state without JS. Important for SEO (search results pages).
  • Persistent across refresh — query params survive F5; React state does not.
// ✅ Filters in URL — shareable, bookmarkable
const [searchParams, setSearchParams] = useSearchParams();
const category = searchParams.get('category') ?? 'all';
const page = Number(searchParams.get('page')) || 1;

// ✅ UI-only state in React — modal open, tooltip hover
const [modalOpen, setModalOpen] = useState(false);

Avoid URL state for: ephemeral UI state (hover, focus, modal open), sensitive data, state that changes many times per second (animation progress).

More State Management Questions

EasyWhat is the difference between server state and client state?EasyWhen would you choose Zustand over Redux Toolkit?EasyWhat is React Query's staleTime vs gcTime (cacheTime) and how do they interact?EasyWhat is optimistic updating and how do you implement it with React Query?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint