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 Input TextBox Component has been created at runtime through button click.

<div id="component-container">
    @DynamicRender
</div>

<button @onclick="RenderComponent">Render TextBox</button>

@code {
    private RenderFragment DynamicRender { get; set; }

    private RenderFragment CreateComponent() => builder =>
    {
        builder.OpenComponent(0, typeof(InputText));
        builder.AddAttribute(1, "Placeholder", "Enter your text");
        builder.CloseComponent();
    };

    private void RenderComponent()
    {
        DynamicRender = CreateComponent();
    }
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

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

Please submit your question and answer.