What React actually does
You write a function that returns a description of some markup. React calls it, compares the result with what is currently on screen, and changes only what differs.
// Not this: find the element and mutate it
document.querySelector("#count").textContent = 5;
// This: describe what it should say, given the data
function Counter() {
const [count, setCount] = useState(0);
return <p id="count">{count}</p>;
}- You never write the update. You change the data and let React find the difference.
- That description is a plain object, not real DOM. React builds the real thing.
- This is why direct DOM edits inside a component get silently undone on the next render.