Previous tip (NavLink gives you more useful navigation links) Next tip (Pass parameters when rendering components dynamically) >>

Dynamic components

Sometimes you want to render different components in your Blazor app based based on their type.

For example, a user-configurable dashboard app with lots of different widgets.

You can do that from .NET 6 onwards using DynamicComponent

<DynamicComponent Type="componentType" />
@code {
private Type componentType = typeof(Counter);
}

More details

Here’s another example whereby you can change the type (to a component called FetchData by clicking the “Change It” button).

@page "/tips/dynamic"
<button @onclick="ChangeType">Change it!</button>
<DynamicComponent Type="componentType" />
@code {
private Type componentType = typeof(Counter);
private void ChangeType()
{
componentType = typeof(FetchData);
}
}