Using RenderFragment
To make our components even more reusable, we can supply them with a piece of Razor syntax. In Blazor, you can specify RenderFragment, which is a fragment of Razor syntax that you can execute and show.
There are two types of render elements, RenderFragment
and RenderFragment<T>
. RenderFragment
is simply a Razor fragment without any input parameters, and RenderFragment<T>
has an input parameter that you can use inside the Razor fragment code by using the context
keyword. We won’t go into depth about how to use this now, but later in this chapter, we will talk about a component (Virtualize) that uses RenderFragment<T>
, and in the next chapter, Chapter 6, Building Forms with Validation, we will implement a component using RenderFragment<T>
.
We can make RenderFragment
the default content inside of the component tags as well as giving it a default value. We will explore this next and build a component using these features.