React State & Updates
If Props are how components talk to each other, State is how a component remembers things for itself. It is the core of interactivity in React.
Local Memory
We introduce state to functional components using the useState hook. It takes the initial state as an argument and returns an array of two items: the current state value, and the function to update it.
Async & Batched Updates
Calling a setter function (like setCount) tells React to schedule a re-render. Because it's asynchronous, if you rely on the current state to calculate the next state, you must use a Functional Update. Passing a callback (e.g. setCount(prev => prev + 1)) guarantees you are working with the latest state.
Immutability Rules
When state is an Object or an Array, do not mutate it directly (e.g., no .push() or user.name = "X"). React checks if state changed by comparing references. Always create a new copy using the spread operator ....
View Full Transcript+
This section covers the transition from class-based this.state to functional hooks. We dive deep into why React batches state updates for performance, avoiding unnecessary DOM repaints. We also emphasize the paradigm of immutability in functional programming, which is crucial for predictable UI updates in React.
