Live Chat Icon For mobile
Live Chat Icon

How to create component dynamically using RenderTreeBuilder in Blazor?

Platform: Blazor| Category: Components

The RenderTreeBuilder class will let you create required content or component in dynamic manner at runtime. In the following code example, the header Component has been created at runtime through RenderTreeBuilder.

[DynamicRenderComponent.cs]

using Microsoft.AspNetCore.Components; 
using Microsoft.AspNetCore.Components.Rendering; 

namespace BlazorServerApp 
{ 
    public class DynamicRenderComponent: ComponentBase 
    { 
        protected override void BuildRenderTree ( RenderTreeBuilder builder ) 
        { 
            builder.OpenElement(0, "h1"); 
            builder.AddContent(0, "This is Example of Dynamically Render Component"); 
            builder.CloseElement(); 
        } 
    } 
}

[Index.Razor] 

@page "/" 
@using Microsoft.AspNetCore.Components.Rendering; 

<DynamicRenderComponent></DynamicRenderComponent> 

@code { 

}

View Sample in GitHub

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.