Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cascade @typeparam in Blazor

Tags:

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>
like image 622
Bogdan B Avatar asked Sep 08 '26 17:09

Bogdan B


2 Answers

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.

like image 85
Douglas Riddle Avatar answered Sep 17 '26 16:09

Douglas Riddle


as of .NET 6.0 and later, you can use the [CascadingTypeParameter] attribute

Parent.razor

@attribute [CascadingTypeParameter(nameof(T))]
@typeparam T

Child.razor

@typeparam T // will be cascaded down from the parent
like image 40
tenxdeveloper Avatar answered Sep 17 '26 16:09

tenxdeveloper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!