Menu

1 / 5

Three Booleans Is Not a State Machine: Modelling UI Logic That Can't Be in Two States at Once

DEV Community: typescript·shinoj cm·about 1 hour ago
#KVCnHVBi
#devtotypescript#devto#dev#machine#states
Reading 0:00
15s threshold

I review a lot of component code. The most common bug pattern I see is not a typo. It is this:

const [ isLoading , setIsLoading ] = useState ( false ) ; const [ isError , setIsError ] = useState ( false ) ; const [ isSuccess , setIsSuccess ] = useState ( false ) ;

Enter fullscreen mode

Exit fullscreen mode

Three booleans, eight combinations, three of which are legal. The other five are bugs waiting for a slow network.

Why the bug is structural, not careless

Everyone knows not to set isLoading and isSuccess at the same time. Nobody sets them deliberately. They happen because the flags are set in different places, on different async paths, sometimes in an await that resolves after the component re-rendered.

Add a quantity and a retryCount and the handshake gets worse.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.