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 {
}
Share with