Skip to content

Commit 363486a

Browse files
authored
Remove lead-in pattern-based examples (#35298)
1 parent f9d5d67 commit 363486a

File tree

1 file changed

+3
-104
lines changed

1 file changed

+3
-104
lines changed

aspnetcore/blazor/components/prerender.md

Lines changed: 3 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,10 @@ To preserve prerendered state, use the `[SupplyParameterFromPersistentComponentS
5353

5454
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>.
5555

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:
5757

58-
```razor
59-
@code {
60-
[SupplyParameterFromPersistentComponentState]
61-
public {TYPE} Data { get; set; }
62-
63-
protected override async Task OnInitializedAsync()
64-
{
65-
Data ??= await ...;
66-
}
67-
}
68-
```
69-
70-
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.
7160

7261
`PrerenderedCounter2.razor`:
7362

@@ -252,51 +241,6 @@ Serialized properties are identified from the actual service instance:
252241

253242
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.
254243

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.
259-
260-
```razor
261-
@implements IDisposable
262-
@inject PersistentComponentState ApplicationState
263-
264-
...
265-
266-
@code {
267-
private {TYPE} data;
268-
private PersistingComponentStateSubscription persistingSubscription;
269-
270-
protected override async Task OnInitializedAsync()
271-
{
272-
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
273-
"{TOKEN}", out var restored))
274-
{
275-
data = await ...;
276-
}
277-
else
278-
{
279-
data = restored!;
280-
}
281-
282-
// Call at the end to avoid a potential race condition at app shutdown
283-
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
284-
}
285-
286-
private Task PersistData()
287-
{
288-
ApplicationState.PersistAsJson("{TOKEN}", data);
289-
290-
return Task.CompletedTask;
291-
}
292-
293-
void IDisposable.Dispose()
294-
{
295-
persistingSubscription.Dispose();
296-
}
297-
}
298-
```
299-
300244
The following counter component example persists counter state during prerendering and retrieves the state to initialize the component.
301245

302246
`PrerenderedCounter3.razor`:
@@ -366,51 +310,6 @@ When the component executes, `currentCount` is only set once during prerendering
366310

367311
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.
368312

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.
373-
374-
```razor
375-
@implements IDisposable
376-
@inject PersistentComponentState ApplicationState
377-
378-
...
379-
380-
@code {
381-
private {TYPE} data;
382-
private PersistingComponentStateSubscription persistingSubscription;
383-
384-
protected override async Task OnInitializedAsync()
385-
{
386-
if (!ApplicationState.TryTakeFromJson<{TYPE}>(
387-
"{TOKEN}", out var restored))
388-
{
389-
data = await ...;
390-
}
391-
else
392-
{
393-
data = restored!;
394-
}
395-
396-
// Call at the end to avoid a potential race condition at app shutdown
397-
persistingSubscription = ApplicationState.RegisterOnPersisting(PersistData);
398-
}
399-
400-
private Task PersistData()
401-
{
402-
ApplicationState.PersistAsJson("{TOKEN}", data);
403-
404-
return Task.CompletedTask;
405-
}
406-
407-
void IDisposable.Dispose()
408-
{
409-
persistingSubscription.Dispose();
410-
}
411-
}
412-
```
413-
414313
The following counter component example persists counter state during prerendering and retrieves the state to initialize the component.
415314

416315
`PrerenderedCounter2.razor`:

0 commit comments

Comments
 (0)