You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/components/prerender.md
+3-104Lines changed: 3 additions & 104 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -53,21 +53,10 @@ To preserve prerendered state, use the `[SupplyParameterFromPersistentComponentS
53
53
54
54
By default, properties are serialized using the <xref:System.Text.Json?displayProperty=fullName> serializer with default settings. Serialization isn't trimmer safe and requires preservation of the types used. For more information, see <xref:blazor/host-and-deploy/configure-trimmer>.
55
55
56
-
The following example demonstrates the general pattern, where the `{TYPE}` placeholder represents the type of data to persist.
56
+
The following counter component persists counter state during prerendering and retrieves the state to initialize the component:
The following counter component example persists counter state during prerendering and retrieves the state to initialize the component.
58
+
* The `[SupplyParameterFromPersistentComponentState]` attribute is applied to the `CounterState` type (`State`).
59
+
* The counter's state is assigned when `null` in `OnInitialized` and restored automatically when the component renders interactively.
71
60
72
61
`PrerenderedCounter2.razor`:
73
62
@@ -252,51 +241,6 @@ Serialized properties are identified from the actual service instance:
252
241
253
242
As an alternative to using the declarative model for persisting state with the `[SupplyParameterFromPersistentComponentState]` attribute, you can use the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service directly, which offers greater flexibility for complex state persistence scenarios. Call <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> to register a callback to persist the component state during prerendering. The state is retrieved when the component renders interactively. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
254
243
255
-
The following example demonstrates the general pattern:
256
-
257
-
* The `{TYPE}` placeholder represents the type of data to persist.
258
-
* The `{TOKEN}` placeholder is a state identifier string. Consider using `nameof({VARIABLE})`, where the `{VARIABLE}` placeholder is the name of the variable that holds the state. Using [`nameof()`](/dotnet/csharp/language-reference/operators/nameof) for the state identifier avoids the use of a quoted string.
The following counter component example persists counter state during prerendering and retrieves the state to initialize the component.
301
245
302
246
`PrerenderedCounter3.razor`:
@@ -366,51 +310,6 @@ When the component executes, `currentCount` is only set once during prerendering
366
310
367
311
To preserve prerendered state, decide what state to persist using the <xref:Microsoft.AspNetCore.Components.PersistentComponentState> service. <xref:Microsoft.AspNetCore.Components.PersistentComponentState.RegisterOnPersisting%2A?displayProperty=nameWithType> registers a callback to persist the component state during prerendering. The state is retrieved when the component renders interactively. Make the call at the end of initialization code in order to avoid a potential race condition during app shutdown.
368
312
369
-
The following example demonstrates the general pattern:
370
-
371
-
* The `{TYPE}` placeholder represents the type of data to persist.
372
-
* The `{TOKEN}` placeholder is a state identifier string. Consider using `nameof({VARIABLE})`, where the `{VARIABLE}` placeholder is the name of the variable that holds the state. Using [`nameof()`](/dotnet/csharp/language-reference/operators/nameof) for the state identifier avoids the use of a quoted string.
0 commit comments