5 concepts · 10 practice questions
Rendering means React calls your component to get a description of the UI. When state changes, that component re-renders, and by default so does every child it renders, whether or not their props changed. React then compares the new tree with the old one and changes only the DOM that differs. The comparison goes by position and element type: the same type at the same place is updated and keeps its state; a different type there is unmounted and a fresh one mounted, losing all state below it.
function Page({ editing }: { editing: boolean }) {
// Switching wrapper type remounts <Form>, so its state resets.
return editing ? <section><Form /></section> : <div><Form /></div>;
}
// Same type in the same spot: state is kept across renders.
function Stable({ editing }: { editing: boolean }) {
return <div className={editing ? "editing" : ""}><Form /></div>;
}
Sign in to read the rest and take the 10-question test.
Sign in to continue