Building reusable Blazor component libraries in .NET 10
Creating reusable component libraries remains one of the best ways to scale Blazor development. .NET 10 brings several improvements that make component libraries cleaner and more powerful.
Create a Razor Class Library
dotnet new razorclasslib -n MyComponents -o MyComponents
Simple reusable component
@* MyButton.razor *@
<button class="btn @CssClass" @onclick="OnClick">
@ChildContent
</button>
@code {
[Parameter]
public string CssClass { get; set; } = "btn-primary";
[Parameter]
public RenderFragment? ChildContent { get; set; }
[Parameter]
public EventCallback OnClick { get; set; }
}
.NET 10 improvements useful for component libraries
1. PersistentState attribute
You can now declaratively persist component state:
[PersistentState]
public string? SelectedValue { get; set; }
2. Better static asset handling
Static files (including CSS isolation) get automatic fingerprinting and compression when served from a Razor Class Library.
3. Improved JavaScript isolation
JavaScript modules from component libraries remain isolated and are easier to consume.
4. Validation improvements
DataAnnotations validation now handles nested types more reliably — useful if your components expose complex models.
Best practices
- Keep components focused
- Use
[Parameter] +
EventCallback
- Prefer CSS isolation (
.razor.css)
- Document public parameters clearly
- Consider the
[PersistentState] attribute when your components hold state across prerendering
Do you maintain your own Blazor component library, or do you mostly use third-party ones?
#dotnet #Blazor #dotnet10 #ASPNETCore #ComponentLibrary #ReusableComponents