
Generic components use the @typeparam directive to create components that work with any data type — similar to C# generics. Ideal for reusable data grids, lists, and dropdowns.
// GenericList.razor
@typeparam TItem
<ul>
@foreach (var item in Items)
{
<li>@ItemTemplate(item)</li>
}
</ul>
@code {
[Parameter] public IEnumerable<TItem> Items { get; set; }
= Enumerable.Empty<TItem>();
[Parameter] public RenderFragment<TItem> ItemTemplate { get; set; } = default!;
}<GenericList Items="products">
<ItemTemplate Context="p">
<strong>@p.Name</strong> — [email protected]
</ItemTemplate>
</GenericList>@typeparam TItem where TItem : class, IIdentifiableBlazor can often infer the type parameter from the data passed in — you don't usually need to specify it explicitly. If inference fails, pass it explicitly: <GenericList TItem="Product" ...>
Reference:
TaskLoco™ — The Sticky Note GOAT