Why useState Isn't Always the Answer: URL State vs React State

Why useState Isn't Always the Answer: URL State vs React State
There's a simple pattern that can change how you handle UI, achieve better performance, maintain SEO, and create a cleaner interface. Learn when to use URL instead of useState.
Mohammad Alhabil
Author
Why useState Isn't Always the Answer: URL State vs React State
There's a simple pattern that can change how you handle UI, help you achieve better performance, maintain SEO, and create a cleaner interface.
The Common Scenario
Imagine this: You have a product list. The user wants to sort by price, category, or newest.
First thought? Use useState:
const [sort, setSort] = useState("latest");
But wait a second...
Does this information really need to live in state? Or should it live in the URL?
The Difference Between the Two
It's like the difference between:
| Approach | Analogy |
|---|---|
| URL | Writing your order on paper and giving it to the waiter |
| State | Whispering it to them and hoping they remember |
With the first approach, anyone with the paper knows what you want.
With the second, only the waiter knows — and if they forget or leave? Your order is gone.
The Solution: Use URL Parameters Instead of useState
Why is the URL Important Here?
1. 🔍 Transparency
The link expresses the page state:
/products?sort=asc&category=shoes
Users can copy the link and return to the exact same state.
2. 💾 Persistence
If they reload or open the same page in another tab? The same data is displayed — nothing gets lost because you didn't rely on useState.
3. 🚀 Performance & SEO
When you use URL with SSR or SSG — the server can prepare data based on the values. Search engines see the correct content, not just an empty page waiting for JavaScript.
Before vs After
❌ Before (useState)
// Client-side only, lost on refresh
const [sort, setSort] = useState("latest");
const [category, setCategory] = useState("all");
const [page, setPage] = useState(1);
// Data fetching depends on state
useEffect(() => {
fetchProducts({ sort, category, page });
}, [sort, category, page]);
✅ After (URL Parameters)
// Next.js App Router
import { useSearchParams } from 'next/navigation';
export default function ProductsPage() {
const searchParams = useSearchParams();
const sort = searchParams.get('sort') || 'latest';
const category = searchParams.get('category') || 'all';
const page = searchParams.get('page') || '1';
// URL is the source of truth
// SSR can read these values on the server
}
When Should You Actually Use useState?
| Use Case | Use useState? |
|---|---|
| Opening a modal | ✅ Yes |
| Hover effects | ✅ Yes |
| Range slider (instant filter) | ✅ Yes |
| Form input before submission | ✅ Yes |
| Pagination | ❌ Use URL |
| Sorting | ❌ Use URL |
| Filtering | ❌ Use URL |
| Search query | ❌ Use URL |
Rule of thumb: If the state determines the main displayed content, it belongs in the URL.
The Benefits
- 📋 Shareable links — Users can share exact page states
- 🔄 Bookmark support — Save filtered views
- ↩️ Back button works — Natural navigation
- 🔍 SEO friendly — Search engines can index different states
- ⚡ SSR compatible — Server can render correct content
The Bottom Line
- Before using useState, ask yourself: Should this information be in the URL?
- Need Pagination, Sort, Filter, Search? Start from the URL, not state.
- Be smart — not everything needs to be stored in useState.
- Focus on user experience, not just implementation.
- React and Next.js support this approach, but few people use it correctly.
Try it in your next project and see the difference yourself!
Interfaces you love, code you understand. 💡
Topics covered
Found this article helpful?
Share it with your network and help others learn too!

Written by Mohammad Alhabil
Frontend Developer & Software Engineer passionate about building beautiful and functional web experiences. I write about React, Next.js, and modern web development.
Related Articles
View all
Props vs State vs Context vs Ref: When to Use Each in React
Confused about when to use Props, State, Context, or Ref? Misusing them doesn't just slow performance—it creates tangled code that's hard to debug. Let's clear things up once and for all.

Managing SVG Icons in React & Next.js: The Complete Guide
Icons from different sources? Legacy SVGs as images? Learn how to properly manage, organize, and use SVG icons in modern React and Next.js projects.

Understanding React Re-renders: Virtual DOM, Diffing & Reconciliation
We're all afraid of re-renders. But is this fear justified? Learn how React actually handles changes under the hood and why not every render is a disaster.