What are generic components in Blazor, and how does the @typeparam directive work?
Last updated
Generic components are reusable UI components typed over one or more type parameters, so the same component works with any data type in a type-safe way (like List<T> for UI).
Declare them with
@typeparam TItem(optionally with awhereconstraint), then useTItemin parameters likeIEnumerable<TItem>andRenderFragment<TItem>.Blazor usually infers the type from supplied parameters. Otherwise specify it explicitly (e.g.
TItem="Person").
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/generic-type-support?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/blazor/components/templated-components?view=aspnetcore-10.0
- https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-10.0
- https://blazor-university.com/templating-components-with-renderfragements/using-typeparam-to-create-generic-components/
- https://levelup.gitconnected.com/types-of-blazor-components-4a81fe6c3f3f
- https://www.michaelmarino.dev/blog/function-parameters/