diff --git a/aspnetcore/blazor/performance.md b/aspnetcore/blazor/performance.md index 24d1528d8fc2..bca4cdd9c3ee 100644 --- a/aspnetcore/blazor/performance.md +++ b/aspnetcore/blazor/performance.md @@ -182,8 +182,6 @@ The preceding example performs well if thousands of messages aren't shown at onc ##### Define reusable `RenderFragments` in code -:::moniker range=">= aspnetcore-6.0" - You might be factoring out child components purely as a way of reusing rendering logic. If that's the case, you can create reusable rendering logic without implementing additional components. In any component's `@code` block, define a . Render the fragment from any location as many times as needed: ```razor @@ -236,81 +234,6 @@ protected RenderFragment DisplayTitle => ; ``` -:::moniker-end - -:::moniker range="< aspnetcore-6.0" - -You might be factoring out child components purely as a way of reusing rendering logic. If that's the case, you can create reusable rendering logic without implementing additional components. In any component's `@code` block, define a . Render the fragment from any location as many times as needed: - -```razor -

Hello, world!

- -@RenderWelcomeInfo - -

Render the welcome info a second time:

- -@RenderWelcomeInfo - -@code { - private RenderFragment RenderWelcomeInfo = __builder => - { -

Welcome to your new app!

- }; -} -``` - -As demonstrated in the preceding example, components can emit markup from code within their `@code` blocks and outside of them. The delegate must accept a parameter called `__builder` of type so that the Razor compiler can produce rendering instructions for the fragment. - -> [!NOTE] -> Assignment to a delegate is only supported in Razor component files (`.razor`), and [event callbacks](xref:blazor/components/event-handling#eventcallback) aren't supported. - -To make code reusable across multiple components, declare the delegate as [`public`](/dotnet/csharp/language-reference/keywords/public) and [`static`](/dotnet/csharp/language-reference/keywords/static): - -```razor -public static RenderFragment SayHello = __builder => -{ -

Hello!

-}; -``` - -`SayHello` in the preceding example can be invoked from an unrelated component. This technique is useful for building libraries of reusable markup snippets that render without per-component overhead. - - delegates can also accept parameters. The following component passes the message (`message`) to the delegate: - -```razor -
- @foreach (var message in messages) - { - @ChatMessageDisplay(message) - } -
- -@code { - private RenderFragment ChatMessageDisplay = message => __builder => - { -
- @message.Author - @message.Text -
- }; -} -``` - -The preceding approach provides the benefit of reusing rendering logic without per-component overhead. However, it doesn't have the benefit of being able to refresh its subtree of the UI independently, nor does it have the ability to skip rendering that subtree of the UI when its parent renders because there's no component boundary. - -For a non-static field, method, or property that can't be referenced by a field initializer, such as `TitleTemplate` in the following example, use a property instead of a field in the delegate: - -```csharp -protected RenderFragment DisplayTitle => __builder => -{ -
- @TitleTemplate -
-}; -``` - -:::moniker-end - #### Don't receive too many parameters If a component repeats extremely often, for example, hundreds or thousands of times, the overhead of passing and receiving each parameter builds up.