Storing Filters in useState: A Bad Idea for Production Apps

WHY STORING FILTERS IN useState IS A BAD IDEA : Filters look like simple UI state. But treating them as local state creates real problems in production apps. What usually happens: Filters are stored using useState Example: • search query • category • price range • page number It works… until users actually use the app. The real problems: 👉 Refresh the page → All filters reset 👉 Copy the URL → No filters included 👉 Share the link → Other user sees different data 👉 Navigate back/forward → State is inconsistent 👉 This breaks user experience in real products The core mistake: Treating filters as UI state But filters are NOT just UI 👉 They represent the current view of data That means: They should live in the URL The correct approach: 👉 URL = Single Source of Truth Instead of: useState → local state Use: URL query params → global state How this works in real apps (Next.js): Use built-in routing tools • useSearchParams() → read filters from URL • useRouter() → update query params Example URL: ?search=laptop&category=electronics&page=2 👉 This URL fully describes the UI state Why this is powerful: • Refresh safe → state persists • Shareable → exact same view • Bookmark friendly • SEO friendly (important for Next.js apps) • Works naturally with browser navigation Real production pattern: When filters change • Update URL params • Reset pagination (page = 1) • Trigger data refetch Where TanStack (React Query) fits: This is where things become clean. Query key depends on URL params Example: ["products", search, category, page] 👉 When URL changes → query key changes → auto refetch No manual state syncing needed Clean architecture flow: URL → Source of truth ↓ React reads params ↓ TanStack Query fetches data ↓ UI renders 👉 No duplicate state 👉 No inconsistencies Important mindset shift: The URL is not just navigation It is: • State • Memory • Shareable context Final Insight: useState is for temporary UI state (not for data that defines the page) If the state affects: • data fetching • navigation • sharing 👉 It belongs in the URL Once this pattern is clear → frontend architecture becomes much cleaner and predictable #ReactJS #NextJS #TanStackQuery #Frontend #WebDevelopment #SystemDesign

  • graphical user interface

To view or add a comment, sign in

Explore content categories