Search code examples
blazor

Cascade @typeparam in Blazor


Can you somehow cascade the @typeparam ?

For instance in this scenario, pass the TItem to the Child without specifying it

Expected behavior:

<Parent TItem="User">
  <Child>
</Parent>

Current:

<Parent TItem="User">
  <Child TItem="User">
</Parent>

Solution

  • Not yet, but it is supposed to be coming with .NET 6.

    From the docs:

    In .NET 6 Preview 2, Blazor can now infer generic type parameters from ancestor components. Ancestor components must opt in to this behavior. An ancestor component can cascade a type parameter by name to descendants using the [CascadingTypeParameter] attribute. This attribute allows generic type inference to use the specified type parameter automatically with descendants that have a type parameter with the same name.