Always Update State Through prev!
prev — Future You Will Thank YouIt is tempting to write setCount(count + 1) and move on. Until a race pops up. Multiple updates in a row, async effects, React 18 batching — and suddenly an older value overwrites the new one. Building the habit of setState(prev => prev + 1) prevents more bugs than you expect.
setState(prev => …)setState calls in one handler all see the latest value.prev points to the newest state, not a stale snapshot.fetch, or await cannot accidentally revert state that another event already changed.prev keeps truth even when renders happen later.prev keeps immutability explicit.prev your default reflexsetValue(value + 1) into setValue(prev => prev + 1) everywhere — even when bugs seem unlikely.setFilters(prev => ({ ...prev, page: prev.page + 1 })) keeps updates immutable.prevconst [likes, setLikes] = useState(0);
const handleLike = () => {
setLikes(prev => prev + 1);
sendAnalyticsEvent('like_clicked');
};Switch it back to setLikes(likes + 1) and fast taps or deferred renders will quietly drop increments. The callback keeps every action accounted for.
Callback state updates are cheap insurance against subtle failures. Spend a few minutes refactoring today and tomorrow metrics, QA, and future-you will be happier. Make prev second nature and your React components stay trustworthy. 💪