Live Chat Icon For mobile
Live Chat Icon

How do I conditionally add Blazor components’ template?

Platform: Blazor| Category: Templated components

The if 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 changes, the condition evaluates to either true or false and adds or removes the component to the page.

Refer to the following code sample.


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

Share with

Related FAQs

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

Please submit your question and answer.