Illia Yershov’s Post

In React, most “state bugs” are caused by state living in the wrong place 😅 Here’s how I decide between usign UI state vs URL state vs server state, without creating a spaghetti code 🍝🇮🇹 1) Local UI state (React useState) Use it when it’s temporary and component-scoped. For typing into an input ⌨️, open/closed modal 🪟, active tab 🗂️, toggle switches ✅. If the user refreshes and it resets and nobody cries, it belongs here. 🔗 2) URL state (route params / query params) Use it when the state should be shareable and navigable: if copy link 📎, bookmark ⭐, Back/Forward ⬅️➡️ should restore the view -> that's the usecase. Search query 🔎, filters 🧰, sorting ↕️, pagination 📄, selected item id 🆔, when it changes what the page “is”, it probably belongs in the URL🌐 3) Remote data state (async fetched data + cache) Use it when the truth lives outside the UI and comes over the network 🌐 Examples: lists/details 📋, permissions 🔐, feature flags ⚙️, anything shared across multiple screens. The “state” here is the client-side handling of it: cache, retries, refetch, stale-time, and invalidation 🔁🧠 React Query/SWR just standardize that so you don’t reinvent it with useEffect + useState everywhere. 🚫 Big rule stays valid: don’t copy fetched data into local React state unless you’re editing it ✍️ — otherwise you create two sources of truth and stale UI is born 😭 My quick rule of thumb: - UI behavior → React state ✅ - View definition/navigation → URL state 🔗 - Backend is the source of truth → remote state 🌐 #React #Frontend #WebDevelopment #JavaScript #TypeScript #StateManagement #ReactQuery #SWR #UX #SoftwareEngineering #WebApp #Developer #ProductEngineering #CleanCode

  • No alternative text description for this image

The URL state point is underrated. If Back/Forward breaks your UX, the state probably lives in the wrong place

The two sources of truth problem with copied remote data is real. Keeping backend data synced with local state manually is asking for stale UI and weird bugs. URL state for anything that defines what the page is showing is a clean mental model that probably saves a lot of headaches.

This is such a clean mental model 👍 I’ve seen so many apps struggle because filters/search lived in local state instead of the URL.

Love this breakdown. Clear mental model, clean boundaries, zero overengineering. As someone working with complex model-driven UIs, I fully agree that state placement is architecture, not syntax

Solid breakdown - your UI/URL/server split nails 90% of state bugs I've debugged.  Last year on a dashboard app, we shoved filters into local state at first, but back/forward broke everything until we yanked them to URL params. Worked like a charm after.  But here's the kicker: what about shared client state like user prefs or theme that spans screens but isn't URL or server? Context or Zustand there, right? Or am I missing something? Been tweaking similar setups lately - check my profile if you want the code snippets

See more comments

To view or add a comment, sign in

Explore content categories