Closed
Description
Summary
Components with InteractiveWebAssembly
render modes cannot receive parameter updates from SSR updates on published apps. If the component receives any changed parameters, it always gets destroyed and re-initialized regardless of whether a matching @key
was present.
Steps to reproduce
dotnet new blazor -int WebAssembly
- Add a
CounterButton
component in the.Client
project:@inject NavigationManager NavigationManager <p role="status">Current count: @currentCount</p> <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> <button class="btn btn-secondary" @onclick="RefreshPage">Refresh</button> @code { private int currentCount = 0; [Parameter] [EditorRequired] public int IncrementAmount { get; set; } private void IncrementCount() { currentCount += IncrementAmount; } private void RefreshPage() { NavigationManager.Refresh(); } }
- Update
Home.razor
:@page "/" <PageTitle>Home</PageTitle> <h1>Hello, world!</h1> <p>Page load count: @pageLoadCount</p> <CounterButton @key="0" @rendermode="InteractiveWebAssembly" IncrementAmount="@pageLoadCount" /> @code { private static int pageLoadCount = 0; protected override void OnInitialized() { pageLoadCount++; } }
- Run the app using
dotnet run
- Click "Click me" and then click "Refresh" and observe that the counter retains its state
- Publish the app using
dotnet publish
- Run the published app
- Click "Click me" and then click "Refresh" and observe that the counter loses its state
Other notes
This appears to be a trimming issue, since doing the following solves the problem:
- Update the
.Client.csproj
file to include:<ItemGroup> <TrimmerRootDescriptor Include="Roots.xml" /> </ItemGroup>
- Add a
Roots.xml
file to the.Client
folder:<linker> <assembly fullname="Microsoft.AspNetCore.Components.WebAssembly" preserve="all"> </assembly> </linker>
.NET SDK Version: 8.0.100
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Done