Temporary State: Counters, Timers, Animations
What you'll learn: When state is short-lived and UI-only, it belongs in local useState — never in global stores.
The Nature of Temporary State
Some state exists only for a fleeting moment: a hover effect that lasts half a second, a loading spinner that disappears when data arrives, an animation flag that toggles on and off. This is temporary state — it appears, does its job, and vanishes without any other component caring.
Think of temporary state like a Post-it note on your desk. You jot down a quick reminder, use it for five minutes, then crumble it up. You wouldn't file that Post-it in a company-wide database. Similarly, a hover indicator doesn't belong in Redux or Context.
What Qualifies as Temporary State
- Counters: Click counts, step numbers in a wizard
- Timers: Countdown values, elapsed time displays
- Animations: "is animating" flags, current frame numbers
- Hover indicators: Boolean flags for
onMouseEnter/onMouseLeave - Loading spinners: Local
isLoadingduring a single fetch
These all share a trait: only one component cares, and the state dies when that component unmounts.
The Local useState Pattern
Keep temporary state exactly where it's needed:
function AnimatedButton() {
const [isHovering, setIsHovering] = useState(false);
const [clickCount, setClickCount] = useState(0);
return (
<button
onMouseEnter={() => setIsHovering(true)}
onMouseLeave={() => setIsHovering(false)}
onClick={() => setClickCount(c => c + 1)}
style={{ transform: isHovering ? 'scale(1.1)' : 'scale(1)' }}
>
Clicked {clickCount} times
</button>
);
}
No Context. No global store. Just local state that appears and disappears with the component.
Key Takeaway: Temporary, UI-only state like counters, timers, and animation flags should live in useState right where they're used — they're too short-lived to justify global state management.