State Updates Trigger Re-renders
What you'll learn: How calling a state setter function causes React to re-render your component with the new value.
The Automatic Update Cycle
When you call a state setter function (like setCount), you're not just changing a variable — you're telling React, "Hey, something important changed. You need to update what's on the screen." React responds by scheduling a re-render of that component.
Here's the flow:
- User clicks a button (or some event happens)
- Your event handler calls
setCount(5) - React schedules a re-render
- Your component function runs again from top to bottom
useStatereturns the new value (5) this time- React updates the DOM to reflect the new output
Why This Matters
This automatic re-rendering is React's superpower. You don't need to manually update the DOM or figure out what changed. You just set the new state value, and React handles the rest.
Think of it like ordering food at a restaurant: you tell the waiter what you want (call the setter), and the kitchen automatically prepares and delivers it (React re-renders). You don't walk into the kitchen yourself.
The Key Mechanism
Every time a setter is called, React treats it as a signal that the component's output might be different now. Since useState returns a new value on the next render, any JSX that depends on that value will automatically show the updated content.
Key Takeaway: Calling a state setter function schedules your component to re-render, causing useState to return the new value and React to update what's displayed on screen.