` contained a `Counter`. But when you swapped in a `p`, React removed the `Counter` from the UI tree and destroyed its state.
-
+
+
+
+
+When Counter changes to p, first the Counter is unmounted, then the p is mounted.
+
+
+
+
+
+
+
+
+
+When switching back, first the p is unmounted, then the Counter is mounted.
+
+
+
+
Also, **when you render a different component in the same position, it resets the state of its entire subtree**. To see how this works, increment the counter and then tick the checkbox:
@@ -620,7 +690,25 @@ label {
The counter state gets reset when you click the checkbox. Although you render a `Counter`, the first child of the `div` changes from a `div` to a `section`. When the child `div` was removed from the DOM, the whole tree below it (including the `Counter` and its state) was destroyed as well.
-
+
+
+
+
+When section changes to div, first the section is unmounted then the new div is mounted.
+
+
+
+
+
+
+
+
+
+When switching back, first the div is unmounted then the new section is mounted.
+
+
+
+
As a rule of thumb, **if you want to preserve the state between re-renders, the structure of your tree needs to "match up"** from one render to another. If the structure is different, the state gets destroyed because React destroys state when it removes a component from the tree.
@@ -741,8 +829,6 @@ h1 {
Currently, when you change the player, the score is preserved. The two `Counter`s appear in the same position, so React sees them as *the same* `Counter` whose `person` prop has changed.
-
-
But conceptually, in this app they should be two separate counters. They might appear in the same place in the UI, but one is a counter for Taylor, and another is a counter for Sarah.
There are two ways to reset state when switching between them:
@@ -827,7 +913,27 @@ h1 {
* Initially, `isPlayerA` is `true`. So the first position contains `Counter` state, and the second one is empty.
* When you click the "Next player" button the first position clears but the second one now contains a `Counter`.
-
+
+
+
+
+Initial State
+
+
+
+
+
+Clicking "next"
+
+
+
+
+
+Clicking "next" again
+
+
+
+
> Each `Counter`'s state gets destroyed each time its removed from the DOM. This is why they reset every time you click the button.
@@ -921,8 +1027,6 @@ Switching between Taylor and Sarah does not preserve the state. This is because
Specifying a `key` tells React to use the `key` itself as part of the position, instead of their order within the parent. This is why, even though you render them in the same place in JSX, from React's perspective, these are two different counters. As a result, they will never share state. Every time a counter appears on the screen, its state is created. Every time it is removed, its state is destroyed. Toggling between them resets their state over and over.
-
-
> Remember that keys are not globally unique. They only specify the position *within the parent*.
### Resetting a form with a key {/*resetting-a-form-with-a-key*/}
@@ -2087,4 +2191,4 @@ State is associated with the tree position. A `key` lets you specify a named pos
-
\ No newline at end of file
+