Skip to content

Extend API for component attributes with render-comparer different from its value #21915

Closed
@Liander

Description

@Liander

To get more control over re-rendering of components it would be good to be able to specify another object for comparisons to determine if an attribute has changed or will lead to another rendering result.

An initial approach could be to just support an extra method in the API for manually building a component for advanced scenarios and leave any tooling changes for later to support it from razor files.

Is your feature request related to a problem? Please describe.

It provides a manual way to get encapsulated component behavior, for performance reasons, or for any other reason (like reliable logging etc.). It is a more built-in way of controlling the re-render condition over the alternative provided now with ShouldRender which is less efficient, leads to different code execution depending on the arguments set and may thus also be sensitive in how you test it.

Describe the solution you'd like

I can see two ways of doing this. The most straightforward is just to recognize that there are cases when the object for testing for equal rendering may not be the same as the attribute value and extend the API to reflect that.

AddAttribute<T, TCompare>(int seq, string name, T value, TCompare attributeRenderComparer) 

UPDATE NOTE: The name render-comparer in the text should not be confused with an IComparer-implementation, it is instead the object that you call Equals on to compare for equal rendering.

Current behavior is achieved by just letting the render-comparer to be the same as the value for the supported types by default, and null otherwise. The internal testing for re-render can then be done by only considering all render-comparers. When manually set one can have the flexibility to completely control the condition for re-render. For example, a RenderFragment type can have a ValueTuple of the dependent values that will produce a different render result, and for event-handlers one might choose just any constant to get them to be static.

Internally it would require the RenderTreeFrame to hold an additional value. Without having studied it in detail it feels like one can use the slot that is now used for AttributeEventUpdatesAttributeName and handle that special situation differently. Perhaps include it in a two-way-binding-handler type instead?

An alternative way can be to have ability to set a flag on an attribute to indicate that it should not participate in the comparison for testing change and have ability set additional 'RenderGuard'-values as another type of render-tree-frames that participate in the test but are not included as attributes to be set as parameters.

The first alternative feels easier and it looks like it can be done by just moving around existing code.

Further described here: #13610 (comment)

Metadata

Metadata

Assignees

Labels

area-blazorIncludes: Blazor, Razor Components

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions