← all posts

FRONTEND

The React Mental Model Nobody Teaches You

Apr 24·React·6 min read

Every React tutorial starts the same way.

"A component is just a function that returns JSX." And technically, yes. But that framing will wreck you the moment your app gets real.

Because components aren't just functions. They're tiny state machines — and once you see them that way, everything changes.

What a state machine actually means

A state machine has states, and rules about how you move between them. Your Button component? States: idle, hovered, loading, disabled, error. Your Modal? Open or closed. Your Form? Untouched, dirty, submitting, success, failed.

React doesn't make these explicit — that's your job. Most bugs happen when developers pretend the states don't exist, then act surprised when the UI ends up in an impossible one.

The bug that taught me this

I had a button that triggered an API call. It had three pieces of state: isLoading, isError, data. I managed them separately. I thought I was being organized.

Then a user clicked the button twice fast. Loading was true. Then error was true. But data was also stale from the last call. The UI showed all three states simultaneously and had a complete meltdown.

The fix? I collapsed it into one state: status: 'idle' | 'loading' | 'success' | 'error'. Now impossible states are literally impossible to represent.

The rule that changed how I write components

If two pieces of state can contradict each other, they shouldn't be two pieces of state.

Before you add a new useState, ask: can this conflict with what I already have? If yes, restructure.

Why debugging gets easier

When you think in state machines, you stop asking "why is the UI broken?" and start asking "how did I end up in this state?" Those are very different questions. The second one has an answer.

You trace the transitions. You find the missing guard. You fix it. Done. Almost enjoyable. I stand by that.

Thanks for reading.Next: Shipping Your First Side Pro… →