Arvind Singh
Arvind Singh

Reputation: 792

What are fragments in Blazor?

Referring to https://learn.microsoft.com/en-us/aspnet/core/blazor/components?view=aspnetcore-3.0#razor-templates what are fragments?

Upvotes: 3

Views: 2480

Answers (1)

enet
enet

Reputation: 45674

A fragment is a chunk or fragment of HtmL content, such as element tags and text, razor mark-up and components, executed in the form of a delegate (RenderFragment)

Here's a simple sample demonstrating this concept:

Index.razor

@page "/"

<h1>Hello, world!</h1>


<TestComponent>
    <p>Get Ready for a new era in Web development</p>
</TestComponent> 

TestComponent.razor

<div>@ChildContent</div>

@code
{
    [Parameter]
    protected RenderFragment ChildContent { get; set; }
}

The above sample describes a parent component named Index, and a nested or child component TestComponent. The content of the nested TestComponent is provided by the parent component, and it is passed to TestComponent in the form of a parameter of RenderFragment delegate. This delegate is invoked in the view portion of the component thus:

<div>@ChildContent</div>

And the html fragment is rendered within the div like this

<div><p>Get Ready for a new era in Web development</p></div>

Hope this helps...

Upvotes: 7

Related Questions