[Blazor] Replacing EditForm Model causes component destruction
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
Reassigning the `Model` of an `EditForm` causes it to create a new `EditContext`, which in turn causes it to destroy and re-create all child components of the form.
### Expected Behavior
Regardless of whether it creates a new `EditContext` or updates it in place, child components should not be destroyed and recreated, they should only have their parameters set and re-rendered.
### Steps To Reproduce
Create a page with:
```
```
Also make a property for `_Model` (of any desired type) and a button that performs something similar to `_Model = new ModelType();`.
Create `InitReporter `:
```
public class InitReporter : ComponentBase, IDisposable
{
[Parameter, EditorRequired] public string? Name { get; set; }
protected override void OnInitialized()
{
Console.WriteLine($"{Name} init");
base.OnInitialized();
}
public void Dispose()
{
Console.WriteLine($"{Name} dispose");
}
protected override void OnParametersSet()
{
Console.WriteLine($"{Name} set parameters");
base.OnParametersSet();
}
}
```
1. Load the page.
2. Observe that top+form init/set are logged as expected.
3. Click the button to assign a different model instance.
4. Observe that the `form` logger reports that it was destroyed and re-created, and the `top` logger does not.
5. Further note that this component isn't even dependent on the model anyway, and yet this still happens -- although even input controls and other things that do depend on the `EditContext` really shouldn't be re-created either.
### Exceptions (if any)
_No response_
### .NET Version
6.0.201
### Anything else?
```
.NET SDK (reflecting any global.json):
Version: 6.0.201
Commit: ef40e6aa06
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\6.0.201\
Host (useful for support):
Version: 6.0.3
Commit: c24d9a9c91
.NET SDKs installed:
2.1.511 [C:\Program Files\dotnet\sdk]
6.0.200 [C:\Program Files\dotnet\sdk]
6.0.201 [C:\Program Files\dotnet\sdk]
.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 6.0.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
```
Contributor guide
Assessment
This issue has not been assessed yet.