From 17d7cfb71ad195329b78fbe0f5216f8a012f9390 Mon Sep 17 00:00:00 2001 From: Luke Latham <1622880+guardrex@users.noreply.github.com> Date: Sun, 21 Nov 2021 06:00:43 -0600 Subject: [PATCH] Persist component state updates (#24013) --- .../prerendering-and-integration.md | 15 ++++++++----- .../built-in/persist-component-state.md | 22 +++++++++++++------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/aspnetcore/blazor/components/prerendering-and-integration.md b/aspnetcore/blazor/components/prerendering-and-integration.md index 3710c9c70f..ead47da98b 100644 --- a/aspnetcore/blazor/components/prerendering-and-integration.md +++ b/aspnetcore/blazor/components/prerendering-and-integration.md @@ -749,7 +749,7 @@ To solve these problems, Blazor supports persisting state in a prerendered page ``` -In the app, decide what state to persist using the service. The [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) event is fired just before the state is persisted into the prerendered page, which allows a component to retrieve the state when initializing the component. +Decide what state to persist using the service. [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes. The following example shows how the weather forecast in the `FetchData` component from a hosted Blazor WebAssembly app based on the Blazor project template is persisted during prerendering and then retrieved to initialize the component. The Persist Component State Tag Helper persists the component state after all component invocations. @@ -797,15 +797,18 @@ else @code { private WeatherForecast[] forecasts = Array.Empty(); - private PersistingComponentStateSubscription _persistingSubscription; + private PersistingComponentStateSubscription persistingSubscription; protected override async Task OnInitializedAsync() { - _persistingSubscription = ApplicationState.RegisterOnPersisting(PersistForecasts); + persistingSubscription = + ApplicationState.RegisterOnPersisting(PersistForecasts); - if (!ApplicationState.TryTakeFromJson("fetchdata", out var restored)) + if (!ApplicationState.TryTakeFromJson( + "fetchdata", out var restored)) { - forecasts = await WeatherForecastService.GetForecastAsync(DateTime.Now); + forecasts = + await WeatherForecastService.GetForecastAsync(DateTime.Now); } else { @@ -822,7 +825,7 @@ else void IDisposable.Dispose() { - _persistingSubscription.Dispose(); + persistingSubscription.Dispose(); } } ``` diff --git a/aspnetcore/mvc/views/tag-helpers/built-in/persist-component-state.md b/aspnetcore/mvc/views/tag-helpers/built-in/persist-component-state.md index fadb3ac3eb..5e90d6412a 100644 --- a/aspnetcore/mvc/views/tag-helpers/built-in/persist-component-state.md +++ b/aspnetcore/mvc/views/tag-helpers/built-in/persist-component-state.md @@ -48,7 +48,7 @@ In Blazor Server apps (`Pages/_Host.cshtml`): ``` -Decide what state to persist using the service. The [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) event is fired just before the state is persisted into the prerendered page, which allows you to retrieve any persisted state when initializing a component. +Decide what state to persist using the service. [`PersistentComponentState.RegisterOnPersisting`](xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A) registers a callback to persist the component state before the app is paused. The state is retrieved when the application resumes. In the following example: @@ -57,19 +57,27 @@ In the following example: ```razor @implements IDisposable -@inject ComponentApplicationState ApplicationState +@inject PersistentComponentState ApplicationState ... @code { + private {TYPE} data; + private PersistingComponentStateSubscription persistingSubscription; + protected override async Task OnInitializedAsync() { - ApplicationState.OnPersisting += PersistData; + persistingSubscription = + ApplicationState.RegisterOnPersisting(PersistData); - if (!ApplicationState - .TryTakeAsJson<{TYPE}>("{TOKEN}", out var data)) + if (!ApplicationState.TryTakeFromJson<{TYPE}>( + "{TOKEN}", out var restored)) { - data = ...; + data = await ...; + } + else + { + data = restored!; } } @@ -82,7 +90,7 @@ In the following example: void IDisposable.Dispose() { - ApplicationState.OnPersisting -= PersistData; + persistingSubscription.Dispose(); } } ```