Radu Catalin-Andrei’s Post

View profile for Radu Catalin-Andrei

🌍 Senior Full-Stack Engineer | B2B SaaS platforms with React, Next.js & Node.js | Headless CMS Integrations | Performance & Multi-Tenant Systems | Scalable Web Architectures | 7+ Years | B2B Contractor

Most React devs handle async state with a handful of booleans. isLoading, isError, data, error. All separate. All synchronized manually. The problem: you can end up in impossible states. isLoading: true AND data present at the same time? Technically possible, logically wrong. A better pattern: discriminated unions in TypeScript. type AsyncState<T> = | { status: "idle" } | { status: "loading" } | { status: "success"; data: T } | { status: "error"; error: string } Now your component switches on state.status. TypeScript narrows the type automatically. No impossible states, no guard clauses scattered everywhere, no "why is data undefined when isLoading is false?" Small shift in thinking, big improvement in code clarity. Once you start modeling state as "what combinations can actually exist," your components get dramatically cleaner. What pattern do you use for async state in React? Still on separate booleans, or have you moved to something like this? #TypeScript #React #WebDevelopment

Or you can use something like TanStack Query which handles this and many more.

Like
Reply

To view or add a comment, sign in

Explore content categories