Skip to content

Add documentation for Blazor generic type cascading feature #21769

Closed
@SteveSandersonMS

Description

@SteveSandersonMS

[EDIT by guardrex to add the metadata]

As of .NET 6 Preview 2, we're adding the ability for generic types to be inferred based on ancestor components. High-level info and motivation is at https://devblogs.microsoft.com/aspnet/asp-net-core-updates-in-net-6-preview-2/#infer-component-generic-types-from-ancestor-components

This can be a fairly brief addition to the docs at https://docs.microsoft.com/en-us/aspnet/core/blazor/components/templated-components?view=aspnetcore-5.0. The key thing we need to document is that by adding @attribute [CascadingTypeParameter(...)] onto a component, the specified generic type argument will automatically be used by descendants that:

  • Are nested as child content for your component in the same .razor document
  • Also declare a @typeparam with the exact same name
  • Don't have any other value supplied or inferred for that type parameter. If they do, that takes precedence over the cascaded generic type.

When receiving a cascaded type parameter, components will obtain it from the closest ancestor that has a [CascadingTypeParameter(...)] with a matching name. In other words, the cascaded generic types parameters can be overridden within a particular subtree.

A caveat worth mentioning is that the matching is done purely by name. So it's not a good idea to cascade a generic type parameter with an excessively - ahem - generic name like T. If a developer opts into cascading a type parameter, they are implicitly promising that its name is unique enough not to clash with other cascaded type parameters from unrelated components.

Is this enough info? I'm happy to supply more details!


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions