Menu

1 / 3

How React Works (Part 6)? How State Actually Works: useState from the Inside

DEV Community·Sam Abaasi·6 months ago
#VxF95iP3
#why#react#javascript#render#count
Reading 0:00
15s threshold

How State Actually Works: useState from the Inside

Series: How React Works Under the Hood

Part 1: Motivation Behind React Fiber: Time Slicing & Suspense

Part 2: Why React Had to Build Its Own Execution Engine

Part 3: How React Finds What Actually Changed

Part 4: The Idea That Makes Suspense Possible

Part 5: The React Lifecycle From the Inside

Prerequisites: Read Parts 1–5 first.

Three Things That Confuse Almost Everyone

Here are three behaviors of useState that trip up developers at every level:

function Counter () { const [ count , setCount ] = useState ( 0 );

function handleClick () { setCount ( count + 1 ); setCount ( count + 1 ); setCount ( count + 1 ); // You might expect count to become 3 // It becomes 1 }

function handleLog () { setCount ( count + 1 ); console .…

Continue reading — create a free account

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