Skip to content

Commit 45bd545

Browse files
authored
[Blazor] Performance - Change detection - set parameters only (#34281)
1 parent e3a1591 commit 45bd545

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

aspnetcore/blazor/performance.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,23 @@ The last two steps of the preceding sequence continue recursively down the compo
3535

3636
To prevent rendering recursion into a particular subtree, use either of the following approaches:
3737

38-
* Ensure that child component parameters are of primitive immutable types, such as `string`, `int`, `bool`, `DateTime`, and other similar types. The built-in logic for detecting changes automatically skips rerendering if the primitive immutable parameter values haven't changed. If you render a child component with `<Customer CustomerId="item.CustomerId" />`, where `CustomerId` is an `int` type, then the `Customer` component isn't rerendered unless `item.CustomerId` changes.
38+
:::moniker range=">= aspnetcore-9.0"
39+
40+
* Ensure that the set parameters of child components are of primitive immutable types, such as `string`, `int`, `bool`, `DateTime`, and other similar types. The built-in logic for detecting changes automatically skips rerendering if the primitive immutable parameter values haven't changed. Only the parameters that are explicitly set on the component are considered for change detection. If you render a child component with `<Customer CustomerId="item.CustomerId" />`, where `CustomerId` is an `int` type, then the `Customer` component isn't rerendered unless `item.CustomerId` changes.
41+
* Override <xref:Microsoft.AspNetCore.Components.ComponentBase.ShouldRender%2A>:
42+
* To accept nonprimitive parameter values, such as complex custom model types or <xref:Microsoft.AspNetCore.Components.RenderFragment> values.
43+
* If authoring a UI-only component that doesn't change after the initial render, regardless of parameter value changes.
44+
45+
:::moniker-end
46+
47+
:::moniker range="< aspnetcore-9.0"
48+
49+
* Ensure that the set parameters of child components are of primitive immutable types, such as `string`, `int`, `bool`, `DateTime`, and other similar types. The built-in logic for detecting changes automatically skips rerendering if the primitive immutable parameter values haven't changed. Only the parameters that are explicitly set on the component are considered for change detection. If you render a child component with `<Customer CustomerId="item.CustomerId" />`, where `CustomerId` is an `int` type, then the `Customer` component isn't rerendered unless `item.CustomerId` changes.
3950
* Override <xref:Microsoft.AspNetCore.Components.ComponentBase.ShouldRender%2A>:
4051
* To accept nonprimitive parameter values, such as complex custom model types, event callbacks, or <xref:Microsoft.AspNetCore.Components.RenderFragment> values.
4152
* If authoring a UI-only component that doesn't change after the initial render, regardless of parameter value changes.
53+
54+
:::moniker-end
4255

4356
The following airline flight search tool example uses private fields to track the necessary information to detect changes. The previous inbound flight identifier (`prevInboundFlightId`) and previous outbound flight identifier (`prevOutboundFlightId`) track information for the next potential component update. If either of the flight identifiers change when the component's parameters are set in [`OnParametersSet`](xref:blazor/components/lifecycle#after-parameters-are-set-onparameterssetasync), the component is rerendered because `shouldRender` is set to `true`. If `shouldRender` evaluates to `false` after checking the flight identifiers, an expensive rerender is avoided:
4457

0 commit comments

Comments
 (0)