Component lifetime
Components internally use a Virtual DOM to efficiently re-render themselves. As components can be nested it is important to understand how the Virtual DOM identifies a component.
Component Identity - Location
If the location of a component (for example in a list of children) changes it is considered a new component.
In the example below we conditionally hide a button. If the button is hidden the location of the random color component below changes. Because the random color component is not considered the same, it is newly created. Therefor the color changes.

Instead of removing the Button from the Virtual DOM we can also just set isVisible accordingly. This does not change the location of the randomColorView and will keep its identity.

Component Identity - Key
A Components identity can be explicitly changed by changing its key. This is useful when the location is stable, but you still want to get a new component in some cases.

If the key of `personEditorView` would not change (see example below) the component would never get re-created.
This is how it looks like if the component key does not change.

Last updated