Sub topic 1
The problem: conditional rendering throws state away
Prerequisites
This chapter assumes you already know the following:
- React function components, props, and JSX.
useStateanduseEffect, including Effect cleanup functions.- The basic idea that React mounts a component when it first appears and unmounts it when it disappears.
- React 19.2 or later, if you want to run the later chapters' examples with
Activity. This chapter's code runs on any recent React.
The pattern everyone starts with
Most React code shows and hides UI with conditional rendering. You write a boolean in state and use && or a ternary in JSX:
import { useState } from 'react';
function Draft() {
const [text, setText] = useState('');
return <textarea value={text} onChange={(e) => setText(e.target.value)} />;
}
export default function App() {
const [showDraft, setShowDraft] = useState(true);
return (
<>
<button onClick={() => setShowDraft(!showDraft)}>Toggle draft</button>
{showDraft && <Draft />}
</>
);
}This works well until a user types something and then hides the component. Try it:
- Type "Dear team," into the textarea.
- Click Toggle draft to hide it.
- Click Toggle draft again to show it.
The textarea is empty. The text is gone.
Why the state disappears
When showDraft becomes false, the expression showDraft && <Draft /> evaluates to false. React renders nothing in that position, so it unmounts Draft. Unmounting does three things:
- React discards the component's state. The
textvalue held byuseStateis thrown away. - React removes the component's DOM nodes from the page. The textarea element no longer exists.
- React runs the cleanup function of every Effect in the component.
When showDraft becomes true again, React mounts a brand-new Draft. It has no memory of the previous one. useState('') runs again and returns the initial value.
React is not misbehaving here. A component's state belongs to its position in the tree. If the position is empty, the state has nowhere to live.
Where this hurts in real apps
The Draft example is small, but the same thing happens in many common interfaces:
| Interface | What the user loses when they switch away |
|---|---|
| Tabbed settings or inbox | Half-filled form fields, filter text |
| Multi-step wizard | Entries from earlier steps |
| Sidebar or collapsible panel | Scroll position, expanded sections |
| Master and detail view | Search query in the list they left |
Users read this as a bug in your app, even though React is behaving as designed. They did not ask to reset anything.
The usual workarounds and their costs
Developers have several ways around this problem. Each has a price.
- Lift state up. Move the
textstate into a parent that stays mounted, and pass it down as props. This works, but the parent now owns state that conceptually belongs to the child. With many panels, the parent fills up with fields it does not care about. - Hide with CSS. Keep the component mounted and set
display: none. State and DOM survive. However, Effects keep running while the component is hidden, so timers tick and subscriptions stay open. The hidden tree also re-renders at the same priority as visible UI. - Store state elsewhere. Put it in a global store,
localStorage, or the URL. This is useful for data that should outlive the page, but it is heavy machinery for a draft that only needs to survive a tab switch.
None of these gives you what you want directly: keep the component's own state and DOM, but make it behave like it is not on screen.
What a better tool would do
Consider what a good solution needs:
- Hiding a component keeps its state and DOM nodes.
- Hiding a component stops its Effects, so it uses no timers or subscriptions in the background.
- Showing it again restores everything instantly, including Effects.
- Hidden content can be prepared in advance and updated without slowing the visible UI.
React's Activity component is built around these four points. The next chapter introduces it.
Check your understanding
Exercise
A user types into a form field inside a component rendered as {isOpen && <Form />}. They close and reopen it, and the field is empty. Name two things React does when isOpen becomes false that explain this, and say what happens when it becomes true again.
Show answer
When isOpen becomes false, React unmounts Form: it discards the component's state (so the typed text is lost) and removes its DOM nodes. It also runs Effect cleanups. When isOpen becomes true again, React mounts a new Form instance, so useState runs with its initial value and the field is empty.
Exercise
You hide a Clock component with display: none instead of unmounting it. Its state survives. What problem remains, and which Effect part causes it?
Show answer
The component is still mounted, so its Effects stay active. A setInterval started in useEffect keeps ticking, and its cleanup function never runs, so the hidden component keeps using resources and updating state in the background.