Most React devs handle API state with three loose pieces: loading, error, and data. Nothing stops them from going out of sync. You can have loading=true and error="something failed" at the same time. Technically impossible in a real app, but TypeScript won't save you. Discriminated unions fix this at the type level. You model the entire async lifecycle as one type with four mutually exclusive shapes: - idle - loading - success, with the typed data - error, with the error message TypeScript narrows it correctly in every branch. Accessing data.results in the error state becomes a compile error. Impossible states are now actually impossible. It feels like a small refactor. In practice it eliminates an entire class of bugs from data-fetching components and makes render logic much cleaner to read. I started using this pattern about 2 years ago. Haven't written the three-booleans approach since. What's your go-to for async state in React: discriminated unions, React Query, or something else? #TypeScript #React
Radu Catalin-Andrei’s Post
More Relevant Posts
-
Your TypeScript compiled with zero errors. Your React app still crashed in production. Here is why. 👇 TypeScript gives us a false sense of security. Here is the trap almost every frontend team falls into: const data = await response.json() as User; The Trap: The TypeScript Mirage 🏜️ By using as User, you just lied to the compiler. TypeScript is a build-time tool. It gets completely stripped away before your code runs in the browser. If your microservice backend team accidentally changes a field, or an API returns an unexpected null, the browser doesn't care about your User interface. The bad data flows directly into your React state, and suddenly your users are staring at a white screen of death because data.map is not a function. The Architectural Fix: Runtime Validation 🛡️ Senior engineers do not trust the network. They build boundaries. Instead of just casting types, you must validate the schema at runtime using a library like Zod. 1️⃣ Define the schema: const UserSchema = z.object({ id: z.string(), name: z.string() }); 2️⃣ Infer the TypeScript type from the schema for your UI. 3️⃣ Parse the incoming data: const user = UserSchema.parse(await response.json()); The Result: If the API sends bad data, Zod throws a clean error at the exact network boundary before it ever touches your React components. You catch the bug at the API layer, not in the UI layer. Are you blindly trusting your API responses, or are you validating at the boundary? 👇 #TypeScript #ReactJS #FrontendEngineering #SoftwareArchitecture #SystemDesign #WebDevelopment #FullStack
To view or add a comment, sign in
-
-
For years, React developers were solving the wrong problem. We dumped everything into Redux, API data, modal states, form inputs, tab selections, and wrote 50+ lines of boilerplate just to fetch some JSON. The real issue? We were confusing server state with client state. Your server state (data from APIs) needs: -Caching -Background refetching -Synchronization across components -Staleness tracking -Request deduplication Your client state (is this modal open?) needs: Simple updates Component-level scope No persistence beyond the session In 2019, Tanner Linsley realized this and built React Query (now TanStack Query). It doesn't fetch anything for you; it's an async state manager that's really good at managing server state. The impact? Most of what we stored in Redux was just cached server data. Once you handle that with React Query, you realize you barely need a global state manager anymore. The actual client state (modals, tabs, form inputs) is small enough to handle with useState and useContext. I wrote about the full evolution: from Redux boilerplate to React Query elegance, and why this shift fundamentally changed how we build React apps. Link in comments. #React #WebDevelopment #JavaScript #StateManagement
To view or add a comment, sign in
-
A production bug that didn’t exist in development… almost cost us real users. The app worked perfectly on localhost. QA passed. No errors. Everything looked stable. But once it went live: • Some users saw outdated data • Others saw duplicate entries • A few actions triggered twice The scary part? We couldn’t reproduce it consistently. After digging deep, I found the root cause: It wasn’t the backend. It wasn’t the API. It was how we handled RxJS streams in Angular. What went wrong: • Multiple subscriptions were created without proper cleanup • Shared data streams were re-triggering API calls • Race conditions between API responses caused inconsistent UI • Lack of caching led to repeated network requests Basically — the app behaved differently under real user conditions. What I changed: • Used shareReplay(1) to cache API responses efficiently • Ensured proper unsubscription using takeUntil pattern • Refactored streams with switchMap to handle race conditions • Centralized API calls into a single service layer • Avoided manual subscriptions wherever possible (used async pipe) The result: • Consistent data across all users • No duplicate API calls • Eliminated random UI glitches • Improved overall app stability in production Big takeaway: In Angular, performance and stability issues often come from how you manage data streams — not just your UI. Mastering RxJS isn’t optional if you want to build production-grade apps. #Angular #RxJS #Frontend #WebDevelopment #Performance #SoftwareEngineering #AngularBug #AngularDebugging
To view or add a comment, sign in
-
React Context is not a state management tool - it's a dependency injection mechanism. Using it for frequently changing global state is silently killing your app's performance. Every time context value updates, ALL consuming components re-render. For small apps this is fine. For growing codebases? It becomes a nightmare. Here's a simple Zustand example that solves this cleanly: import { create } from 'zustand' const useStore = create((set) => ({ user: null, setUser: (user) => set({ user }), })) Only components that subscribe to specific slices re-render. No unnecessary updates. No performance headaches. When to use what: - Context: themes, locale, auth flags - Zustand: UI state, shared business logic - Redux: complex apps needing strict data flow and middleware This principle applies across stacks too - whether you're working in React, integrating with a Node.js backend, or consuming APIs from .NET/C# services, clean state architecture matters end-to-end. Keep your Context lean. Let dedicated tools handle the heavy lifting. What's your go-to state management solution in 2026? #React #JavaScript #WebDevelopment #Frontend #Zustand #SoftwareEngineering
To view or add a comment, sign in
-
🚀 We just open-sourced a React hook for free reverse geocoding. The problem: Adding location awareness to a React app typically means signing up for a geocoding service, managing API keys, handling GPS/IP fallback logic, and normalising different response formats. The solution: One hook. import { useLocation } from '@bigdatacloudapi/react-reverse-geocode-client'; const { data } = useLocation(); // data.city, data.countryName, data.postcode — done. What makes it different: • No API key needed — works out of the box • GPS with automatic IP geolocation fallback • Identical response format regardless of detection method • 100+ language support for locality names • Full TypeScript types • SSR/Next.js compatible Powered by BigDataCloud's patented IP geolocation technology, benchmarked daily against competitors in our public accuracy report. 📦 npm install @bigdatacloudapi/react-reverse-geocode-client 💻 https://lnkd.in/grfUrSdw 📖 https://lnkd.in/gfTGveib #react #javascript #webdev #geolocation #opensource #typescript #developer
To view or add a comment, sign in
-
I often see frontend performance issues that start as a misunderstanding of boundaries, not a flaw in React or Next.js. The pattern is consistent: server-side data fetching, client-side state, and API orchestration logic get tangled within the same component tree. This creates a cascade of unnecessary re-renders and makes loading states difficult to manage. The problem isn't the framework; it's the architecture. We addressed this by enforcing strict server-client separation in a Next.js 14 application. We moved all initial data fetching and complex computations into Server Components and React `cache()`. Mutations and real-time updates were channeled through stable, dedicated API routes built with the App Router. The key was instrumenting the hydration phase. Using the React DevTools Profiler and custom logging, we measured the cost of client-side JavaScript before optimizing. This revealed that much of the perceived slowness was from over-fetching and re-rendering context providers, not from the server render itself. The result is a clearer mental model and a faster application. Performance gains came from making intentional choices about what runs where, not from micro-optimizations. #NextJS #WebPerformance #React #SoftwareArchitecture #FrontendEngineering #DeveloperExperience #TypeScript #Vercel
To view or add a comment, sign in
-
🚨 Infinite API Calls: The Silent Killer of Your App Everything looks fine… until your API starts getting hammered with requests nonstop 😬 If you’ve worked with React (or any frontend framework), chances are you’ve faced this at least once. 🔴 The Problem: Your component keeps calling an API again… and again… and again. ➡️ Backend gets overloaded ➡️ UI becomes sluggish ➡️ Rate limits get hit ➡️ Users suffer Most of the time, it comes down to a simple mistake. ⚠️ Common Causes: Missing dependency array in useEffect: Runs on every render Incorrect dependencies: State updates trigger the effect repeatedly Updating state inside the same effect that depends on it: Creates a loop Functions recreated on every render: Triggers effect again ⚡ How to fix it: ✅ 1. Use Dependency Array Correctly useEffect(() => { fetchData(); }, []); // runs only once ✅ 2. Be Careful with State Updates Avoid updating a state that is also in your dependency array unless necessary. ✅ 3. Memoize Functions const fetchData = useCallback(() => { // API call }, []); ✅ 4. Use Flags / Conditions if (!data) { fetchData(); } ✅ 5. Use Libraries Tools like React Query or SWR handle caching, refetching, and prevent unnecessary calls. 💡 Pro Tip: If your API tab in DevTools looks like a machine gun 🔫, you’ve got an infinite loop. Small mistake. Big impact. Debugging this once will make you a better developer forever. Have you ever run into infinite API calls? How did you fix it? 👇 #ReactJS #WebDevelopment #FrontendDevelopment #JavaScript #SoftwareEngineering #Debugging #ProgrammingTips #CleanCode #DevCommunity #ReactDeveloper #WebDevTips #API #PerformanceOptimization #CodeQuality #TechTips
To view or add a comment, sign in
-
-
🚀 Understanding Props vs State in React — Simplified! In React, everything revolves around data. But there are two ways to handle it: 👉 Props 👉 State Understanding the difference is crucial for building scalable apps. 💡 What are Props? 👉 Props (short for properties) are used to pass data from parent to child function Greeting({ name }) { return <h1>Hello {name}</h1>; } <Greeting name="React" /> ✅ Read-only ✅ Passed from parent ✅ Cannot be modified by child 💡 What is State? 👉 State is data managed inside a component const [count, setCount] = useState(0); setCount(count + 1); ✅ Mutable ✅ Managed within component ✅ Triggers re-render ⚙️ How it works 🔹 Props: Flow: Parent → Child Immutable Used for communication 🔹 State: Local to component Can be updated Controls UI behavior 🧠 Real-world use cases ✔ Props: Passing data to components Reusable UI components Configuring child behavior ✔ State: Form inputs Toggle UI (modals, dropdowns) Dynamic data 🔥 Best Practices (Most developers miss this!) ✅ Use props for passing data ✅ Use state for managing UI ✅ Keep state minimal and local ❌ Don’t mutate props directly ❌ Don’t duplicate state unnecessarily ⚠️ Common Mistake // ❌ Mutating props props.name = "New Name"; 👉 Props are read-only → always treat them as immutable 💬 Pro Insight 👉 Props = External data (controlled by parent) 👉 State = Internal data (controlled by component) 📌 Save this post & follow for more deep frontend insights! 📅 Day 8/100 #ReactJS #FrontendDevelopment #JavaScript #ReactHooks #WebDevelopment #SoftwareEngineering #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
🚨 Most React developers misuse "useEffect" And it’s slowing down their apps. Here’s the mistake 👇 useEffect(() => { fetch("/api/products") }, []) Looks correct, right? But this pattern becomes a problem in real applications. Why? Because developers start putting everything inside useEffect: ❌ API calls ❌ data transformations ❌ business logic ❌ state syncing Result: • messy components • hard-to-debug code • unnecessary re-renders 💡 Better approach: 👉 Move logic OUT of components 👉 Create a service layer 👉 Use proper data fetching tools (React Query, etc.) Example: const { data } = useQuery("products", fetchProducts) Now your component becomes: ✔ cleaner ✔ easier to maintain ✔ more scalable 💡 "useEffect" is not for everything. It’s only for side effects that truly belong to the component. #reactjs #frontend #javascript #softwareengineering #webdevelopment
To view or add a comment, sign in
-
-
I was facing a performance issue in my React application. The problem was simple: Unnecessary API calls. Every time a user navigated between pages, the same APIs were being called again — even when the data hadn’t changed. This led to: • Slower UI • Increased load time • Unnecessary backend load The solution? I replaced useEffect with React Query. Here’s what changed: • API responses started getting cached • No repeated API calls on navigation • Instant UI updates when revisiting pages • No need to manually manage loading and error states Example: User visits Page 1 → Page 2 → back to Page 1 👉 Instead of calling API again, cached data is returned instantly This significantly improved performance — from seconds to milliseconds. This is how modern React applications handle data fetching. 👉 https://lnkd.in/gpc2mqcf 💬 Comment REACT and I’ll share the detailed React Query documentation. #ReactJS #ReactQuery #FrontendEngineering #WebDevelopment #SoftwareEngineering #PerformanceOptimization #JavaScript
Stop Using useEffect for API Calls
To view or add a comment, sign in
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development