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/cascading-values-and-parameters.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -517,4 +517,4 @@ The following `ExampleTabSet` component uses the `TabSet` component, which conta
517
517
## Additional resources
518
518
519
519
*[Generic type support: Explicit generic types based on ancestor components](xref:blazor/components/generic-type-support#explicit-generic-types-based-on-ancestor-components)
520
-
*[State management: Factor out the state preservation to a common location](xref:blazor/state-management?pivots=server#factor-out-the-state-preservation-to-a-common-location)
520
+
*[State management: Factor out state preservation to a common provider](xref:blazor/state-management?pivots=server#factor-out-state-preservation-to-a-common-provider)
Copy file name to clipboardExpand all lines: aspnetcore/blazor/state-management.md
+39-16Lines changed: 39 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -399,11 +399,17 @@ else
399
399
400
400
:::moniker-end
401
401
402
-
### Factor out the state preservation to a common location
402
+
### Factor out state preservation to a common provider
403
403
404
404
If many components rely on browser-based storage, implementing state provider code many times creates code duplication. One option for avoiding code duplication is to create a *state provider parent component* that encapsulates the state provider logic. Child components can work with persisted data without regard to the state persistence mechanism.
405
405
406
-
In the following example of a `CounterStateProvider` component, counter data is persisted to `sessionStorage`:
406
+
In the following example of a `CounterStateProvider` component, counter data is persisted to `sessionStorage`, and it handles the loading phase by not rendering its child content until state loading is complete.
407
+
408
+
The `CounterStateProvider` component deals with prerendering by not loading state until after component rendering in the [`OnAfterRenderAsync` lifecycle method](xref:blazor/components/lifecycle#after-component-render-onafterrenderasync), which doesn't execute during prerendering.
409
+
410
+
The approach in this section isn't capable of triggering rerendering of multiple subscribed components on the same page. If one subscribed component changes the state, it rerenders and can display the updated state, but a different component on the same page displaying that state displays stale data until its own next rerender. Therefore, the approach described in this section is best suited to using state in a single component on the page.
> For more information on <xref:Microsoft.AspNetCore.Components.RenderFragment>, see <xref:blazor/components/index#child-content-render-fragments>.
491
519
492
-
The `CounterStateProvider` component handles the loading phase by not rendering its child content until state loading is complete.
493
-
494
520
:::moniker range=">= aspnetcore-8.0"
495
521
496
522
To make the state accessible to all components in an app, wrap the `CounterStateProvider` component around the <xref:Microsoft.AspNetCore.Components.Routing.Router> (`<Router>...</Router>`) in the `Routes` component with global interactive server-side rendering (interactive SSR).
@@ -541,17 +567,14 @@ Wrapped components receive and can modify the persisted counter state. The follo
541
567
{
542
568
if (CounterStateProvider is not null)
543
569
{
544
-
CounterStateProvider.CurrentCount++;
545
-
await CounterStateProvider.SaveChangesAsync();
570
+
await CounterStateProvider.IncrementCount();
546
571
}
547
572
}
548
573
}
549
574
```
550
575
551
576
The preceding component isn't required to interact with `ProtectedBrowserStorage`, nor does it deal with a "loading" phase.
552
577
553
-
To deal with prerendering as described earlier, `CounterStateProvider` can be amended so that all of the components that consume the counter data automatically work with prerendering. For more information, see the [Handle prerendering](#handle-prerendering) section.
554
-
555
578
In general, the *state provider parent component* pattern is recommended:
0 commit comments