Dynamic Component in Blazor

Dynamic Component in Blazor

This blog is going to explain what a Dynamic Component is and how to use it. It is a built-in component used to render components by type. The Dynamic Component has two arguments which are type and parameter. Type is a component type and parameter is the parameter to be sent to the component.

Let's see how to implement Dynamic Components in Blazor. Below is a basic example of how to implement Dynamic Components. The "typeof(Counter)" is assigned to the Type property. It will render the Counter component in the index page.

index.razor
@page "/"
 
<h3>Dynamic Component Demo</h3>
<h4>Index Page</h4>
 
<DynamicComponent Type="typeof(Counter)" />

The following image is the output of the code snippets above. You can see here the Counter Component is rendered by Dynamic Component.

Dynamic Componenet

Pass Parameter

Let us see how to send the parameter to the component. The Dynamic Component sends the parameter as IDictionary<string, object>. The string is the name of the parameter and the object is the value of the parameter.

The following is a counter.razor component. Here I have added two property variables Title and current number with [parameter] attribute. These property variables receive values from Dynamic Components.

Counter.razor
@page "/counter"
 
<PageTitle>Counter</PageTitle>
 
<h1>@Title</h1>
 
<p role="status">Current count: @currentCount</p>
 
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
 
@code {
    [Parameter]
    public string Title { getset; }
    [Parameter]
    public int currentCount { getset; }
 
    private void IncrementCount()
    {
        currentCount++;
    }
}

In the below coding the parameters are passed as a dictionary.

index.razor
@page "/"
 
<h3>Dynamic Component Demo</h3>
<h4>Index Page</h4>
 
<DynamicComponent Type="typeof(Counter)"
    Parameters="@(new Dictionary<string, object>(){
                  {"Title", "Counter Demo"},
                  {"currentCount", 20}})" />

The following image is the output of the code snippets above. In the image you can see the title "Counter Demo" and the current count "20" is displayed.

Dynamic Component with Parameters

Let’s see another example. Here I have created a ComponentMetadata.cs class file.

ComponentMetadata.cs
public class ComponentMetadata
{
  public Dictionary<stringobjectParameters { getset; } = new();
}

In the below code, a dropdown is added to select a required component name. In the select element option I have added Counter and FetchData components.

index.razor
@page "/"
 
<select @onchange="dropdownChange">
            <option value="">Select a value</option>
            <option value="@nameof(Counter)">Counter</option>
            <option value="@nameof(FetchData)">FetchData</option>
</select>
 
@if(type is not null)
{
        <DynamicComponent Type="type" Parameters="@components[type.Name].Parameters"/>
}
 
@code {
        private Dictionary<stringComponentMetadatacomponents =
        new()
        {
            {
                "Counter",
                new ComponentMetadata
                {
                    Parameters = new Dictionary<stringobject>(){{"Title""Counter Demo"},
                                                                  {"currentCount"10}}
                }
            },
            {
                "FetchData",
                new ComponentMetadata
                {
                    Parameters = new Dictionary<stringobject>(){{"Title""Weather forecast"}}
                }
            }
        };
 
    private Typetype;
 
    private void dropdownChange(ChangeEventArgs e)
    {
        type = e.Value?.ToString()?.Length > 0  ? Type.GetType($"SampleBlazorApp.Pages.{e.Value}") : null;        
    }
}

The output of the code above is below. As you can see here, when the user selects a component from the drop-down, it renders to the page with its parameter.

Dynamic Componet in Blazor

I hope this helps you. Keep coding.

Comments

Popular posts from this blog

Entity Framework Core (EF) with SQL Server LocalDB

Exploring EventCallback in Blazor: Building Interactive Components

A Step-by-Step Guide to Implementing Identity in ASP.NET Core MVC