Skip to content

Commit 58661d4

Browse files
committed
Declarative persistent component state updates
1 parent 9d68074 commit 58661d4

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

aspnetcore/blazor/components/integration.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ In the following example, the `{TYPE}` placeholder represents the type of data t
416416
@code {
417417
[SupplyParameterFromPersistentComponentState]
418418
public {TYPE} Data { get; set; }
419+
420+
protected override async Task OnInitializedAsync()
421+
{
422+
Data ??= await ...;
423+
}
419424
}
420425
```
421426

aspnetcore/blazor/components/lifecycle.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ else
646646
647647
private async Task<string> LoadDataAsync()
648648
{
649-
await Task.Delay(10000);
649+
await Task.Delay(5000);
650650
return "Finished!";
651651
}
652652
}
@@ -701,7 +701,7 @@ else
701701
702702
private async Task<string> LoadDataAsync()
703703
{
704-
await Task.Delay(10000);
704+
await Task.Delay(5000);
705705
return "Finished!";
706706
}
707707

aspnetcore/blazor/components/prerender.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,32 @@ The following counter component example persists counter state during prerenderi
8989
9090
protected override void OnInitialized()
9191
{
92-
CurrentCount ??= Random.Shared.Next(100);
93-
Logger.LogInformation("CurrentCount set to {Count}", CurrentCount);
92+
if (CurrentCount == 0)
93+
{
94+
CurrentCount = Random.Shared.Next(100);
95+
Logger.LogInformation("CurrentCount set to {Count}", CurrentCount);
96+
}
97+
else
98+
{
99+
Logger.LogInformation("CurrentCount restored to {Count}", CurrentCount);
100+
}
101+
94102
}
95103
96104
private void IncrementCount() => CurrentCount++;
97105
}
98106
```
99107

108+
When the component executes, `CurrentCount` is only set once during prerendering. The value is restored when the component is rerendered. The following is example output.
109+
110+
> [!NOTE]
111+
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
112+
113+
> :::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter2[0]":::
114+
> :::no-loc text=" CurrentCount set to 96":::
115+
> :::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter2[0]":::
116+
> :::no-loc text=" CurrentCount restored to 96":::
117+
100118
In the following example that serializes state for multiple components of the same type:
101119

102120
* Properties annotated with the `[SupplyParameterFromPersistentComponentState]` attribute are serialized and deserialized during prerendering.
@@ -283,6 +301,16 @@ The following counter component example persists counter state during prerenderi
283301
}
284302
```
285303

304+
When the component executes, `currentCount` is only set once during prerendering. The value is restored when the component is rerendered. The following is example output.
305+
306+
> [!NOTE]
307+
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
308+
309+
> :::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter3[0]":::
310+
> :::no-loc text=" currentCount set to 96":::
311+
> :::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter3[0]":::
312+
> :::no-loc text=" currentCount restored to 96":::
313+
286314
:::moniker-end
287315

288316
:::moniker range="< aspnetcore-10.0"
@@ -340,18 +368,18 @@ The following counter component example persists counter state during prerenderi
340368

341369
:::code language="razor" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Components/Pages/PrerenderedCounter2.razor":::
342370

343-
:::moniker-end
344-
345371
When the component executes, `currentCount` is only set once during prerendering. The value is restored when the component is rerendered. The following is example output.
346372

347373
> [!NOTE]
348-
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the `PrerenderedCounter2` component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
374+
> If the app adopts [interactive routing](xref:blazor/fundamentals/routing#static-versus-interactive-routing) and the page is reached via an internal [enhanced navigation](xref:blazor/fundamentals/routing#enhanced-navigation-and-form-handling), prerendering doesn't occur. Therefore, you must perform a full page reload for the component to see the following output. For more information, see the [Interactive routing and prerendering](#interactive-routing-and-prerendering) section.
349375
350376
> :::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter2[0]":::
351377
> :::no-loc text=" currentCount set to 96":::
352378
> :::no-loc text="info: BlazorSample.Components.Pages.PrerenderedCounter2[0]":::
353379
> :::no-loc text=" currentCount restored to 96":::
354380
381+
:::moniker-end
382+
355383
By initializing components with the same state used during prerendering, any expensive initialization steps are only executed once. The rendered UI also matches the prerendered UI, so no flicker occurs in the browser.
356384

357385
The persisted prerendered state is transferred to the client, where it's used to restore the component state. During client-side rendering (CSR, `InteractiveWebAssembly`), the data is exposed to the browser and must not contain sensitive, private information. During interactive server-side rendering (interactive SSR, `InteractiveServer`), [ASP.NET Core Data Protection](xref:security/data-protection/introduction) ensures that the data is transferred securely. The `InteractiveAuto` render mode combines WebAssembly and Server interactivity, so it's necessary to consider data exposure to the browser, as in the CSR case.

0 commit comments

Comments
 (0)