React › Props & State

Why is state considered the single source of truth for data that changes in a React component?

Intermediate

Answer

Keeping data in state rather than scattered across variables or the actual DOM ensures React always knows the current, correct value, and can reliably re render the screen to match it. If the same piece of data existed in more than one place, they could easily fall out of sync and show incorrect or confusing information.
// Risky, two separate places tracking similar information
let isOpenVariable = false; // a plain variable, React does not track this
const [isOpen, setIsOpen] = useState(false); // the actual source of truth

// Correct, only state controls whether the menu is shown
return isOpen && <Menu />;
Real-world example A dropdown menu component avoids a confusing bug where the menu sometimes appeared closed in the UI but was technically still open in the code, by removing a leftover plain variable and relying only on state as the single source of truth.

Common follow-ups: What problems can happen if the same piece of information is tracked in two different places?;How do you decide whether a piece of data should live in state at all?