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.
typeViews()=static let random = Random()static let randomColor ()= String.Format("#{0:X6}", random.Next(0x1000000))static member randomColorView ()= Component.create ("randomColorView",fun ctx ->let color = ctx.useState (randomColor()) TextBlock.create [ TextBlock.background color.Current TextBlock.text $"Color {color.Current}"])static member mainView ()= Component (fun ctx ->let isHidden = ctx.useState false DockPanel.create [ DockPanel.lastChildFill true DockPanel.children [ifnot isHidden.Current then Button.create [ Button.content "hide button" Button.onClick (fun _ -> isHidden.Set true)] Views.randomColorView ()]])
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.
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.
// BUG - component is never re-createdComponent.create ("edit-person",fun ctx ->..)// WORKS - component is re-created when person changesComponent.create ($"edit-person-{person.Id}",fun ctx ->..)
This is how it looks like if the component key does not change.