Blazor

How do I conditionally add Blazor components’ template?

How do I conditionally add Blazor components’ template?

The if the statement is used to display a component or fragment conditionally. If the condition evaluates to true, the component will be loaded to the view.

Whenever the properties used in the if statement change

<button class="btn" @onclick="ButtonClicked">Show</button>
 
@if (ShowComponent)
{
    <FetchData></FetchData>
}
 
@code{
    public bool ShowComponent { get; set; }
 
    public void ButtonClicked(MouseEventArgs e)
    {
        ShowComponent = true;
    }
}

, the condition evaluates to either true or false and adds or removes the component to the page.

Refer to the following code sample.

Sumitra Kumari Shaw

An Engineering graduate who Enjoys building things that live on the internet.