Unmounting Destroys State
You'll learn what happens to a component's state when it's removed from the React tree—and why remounting doesn't remember the old state.
The Component Lifecycle and State Storage
Remember that state lives in the React tree, not in your component function itself. React maintains this state as long as the component remains mounted in the tree. But here's the critical point: when a component is unmounted (removed from the tree), React throws away all its state.
If that same component is later remounted—even in the exact same position—it starts completely fresh with its initial state value. It's not the same instance anymore; it's a brand new one.
A Real-World Analogy
Think of a hotel room. While you're checked in (mounted), the hotel stores your belongings (state). When you check out (unmount), housekeeping clears everything out. If you return next week and book the same room number (remount), you don't find your old stuff there—the room is reset to its default, clean state.
Why This Matters
This behavior explains several common scenarios:
- Conditional rendering: If you conditionally show/hide a component with
{condition && <Counter />}, hiding it destroys its state. Showing it again creates a freshCounterwith initial state. - Navigating between routes: When you leave a page, its components unmount and lose state. Returning to that page starts fresh unless you store state elsewhere.
- Removing items from lists: If a list item component is removed, any state it held is gone forever.
This is by design—it prevents memory leaks and stale state from lingering in your application.
Key Takeaway: When a component unmounts, React destroys its state completely. Remounting that component creates brand new state from the initial value—there's no memory of what came before.