Skip to content

Commit 5a3d7b2

Browse files
authored
Cascading values updates (#31488)
1 parent 890e16b commit 5a3d7b2

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

aspnetcore/blazor/components/cascading-values-and-parameters.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ The following class is used in this section's examples.
3333

3434
:::code language="csharp" source="~/../blazor-samples/8.0/BlazorSample_BlazorWebApp/Dalek.cs":::
3535

36-
The following registrations are made in the app's `Program` file:
36+
The following registrations are made in the app's `Program` file with <xref:Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.AddCascadingValue%2A>:
3737

3838
* `Dalek` with a property value for `Units` is registered as a fixed cascading value.
3939
* A second `Dalek` registration with a different property value for `Units` is named "`AlphaGroup`".
@@ -60,6 +60,13 @@ builder.Services.AddCascadingValue(sp =>
6060
});
6161
```
6262

63+
> [!WARNING]
64+
> Registering a component type as a root-level cascading value doesn't register additional services for the type or permit service activation in the component.
65+
>
66+
> Treat required services separately from cascading values, registering them separately from the cascaded type.
67+
>
68+
> Avoid using <xref:Microsoft.Extensions.DependencyInjection.CascadingValueServiceCollectionExtensions.AddCascadingValue%2A> to register a component type as a cascading value. Instead, wrap the `<Router>...</Router>` in the `Routes` component (`Components/Routes.razor`) with the component and adopt global interactive server-side rendering (interactive SSR). For an example, see the [`CascadingValue` component](#cascadingvalue-component) section.
69+
6370
:::moniker-end
6471

6572
## `CascadingValue` component
@@ -468,3 +475,8 @@ The following `ExampleTabSet` component uses the `TabSet` component, which conta
468475
private bool showThirdTab;
469476
}
470477
```
478+
479+
## Additional resources
480+
481+
* [Generic type support: Explicit generic types based on ancestor components](xref:blazor/components/generic-type-support#explicit-generic-types-based-on-ancestor-components)
482+
* [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)

aspnetcore/blazor/state-management.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,26 @@ else
493493
494494
The `CounterStateProvider` component handles the loading phase by not rendering its child content until state loading is complete.
495495

496+
:::moniker range=">= aspnetcore-8.0"
497+
498+
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).
499+
500+
In the `App` component (`Components/App.razor`):
501+
502+
```razor
503+
<Routes @rendermode="InteractiveServer" />
504+
```
505+
506+
In the `Routes` component (`Components/Routes.razor`):
507+
508+
:::moniker-end
509+
510+
:::moniker range="< aspnetcore-8.0"
511+
496512
To use the `CounterStateProvider` component, wrap an instance of the component around any other component that requires access to the counter state. To make the state accessible to all components in an app, wrap the `CounterStateProvider` component around the <xref:Microsoft.AspNetCore.Components.Routing.Router> in the `App` component (`App.razor`):
497513

514+
:::moniker-end
515+
498516
```razor
499517
<CounterStateProvider>
500518
<Router ...>
@@ -773,8 +791,6 @@ When implementing custom state storage, a useful approach is to adopt [cascading
773791
* To consume state across many components.
774792
* If there's just one top-level state object to persist.
775793

776-
For additional discussion and example approaches, see [Blazor: In-memory state container as cascading parameter (dotnet/AspNetCore.Docs #27296)](https://github.com/dotnet/AspNetCore.Docs/issues/27296).
777-
778794
## Troubleshoot
779795

780796
In a custom state management service, a callback invoked outside of Blazor's synchronization context must wrap the logic of the callback in <xref:Microsoft.AspNetCore.Components.ComponentBase.InvokeAsync%2A?displayProperty=nameWithType> to move it onto the renderer's synchronization context.

0 commit comments

Comments
 (0)