Rendering optimization with ShouldRender
In order to introduce the ShouldRender
method and explain how to use it, first, we need to understand how parameters and events can affect the rendering of components in Blazor.
Blazor components exist in a hierarchy, with a root component that has child components, each child component can have its own child components, and so on. The re-render happens in the following scenario:
- When a component receives an event or a parameter changes, it re-renders itself and passes a new set of parameter values to its child components
- Each child component decides whether to re-render or not based on the type and value of the parameter values it receives:
- If the parameter values are primitive types (such as
string
,int
,DateTime
, andbool
) and they have not changed, the child component does not re-render - If the parameter values are non-primitive types (such as complex models, event callbacks, or
RenderFragment
values) or they have changed, the...
- If the parameter values are primitive types (such as