Skip to content

Commit

Permalink
Remove ShouldRender optimisations as they lead to incorrect behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper committed Sep 14, 2023
1 parent d765f2b commit ae2b22e
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal class DataTemplateItemComponent<T> : ComponentBase
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
{
private object _item;
private bool _shouldRender = true;

[Parameter] public RenderFragment<T> Template { get; set; }

Expand Down Expand Up @@ -45,20 +44,11 @@ public override Task SetParametersAsync(ParameterView parameters)
return base.SetParametersAsync(ParameterView.Empty);
}

protected override bool ShouldRender()
{
// Re-rendering is required only if BindingContext is changed.
// If this method is not overridden, it re-renders all items in DataTemplateItemsComponent
// when new item is added there.
return _shouldRender;
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (_item != null)
{
builder.AddContent(0, Template.Invoke((T)_item));
_shouldRender = false;
}
}

Expand All @@ -72,7 +62,6 @@ private void OnContentViewSet()
if (newItem != null && newItem != _item)
{
_item = newItem;
_shouldRender = true;
StateHasChanged();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace BlazorBindings.Maui.Elements.DataTemplates;
internal class SyncDataTemplateItemComponent<T> : ComponentBase
{
private T _item;
private bool _shouldRender = true;
private bool _initialValueSet;

public MC.BindableObject RootControl { get; private set; }
Expand Down Expand Up @@ -43,14 +42,6 @@ public override Task SetParametersAsync(ParameterView parameters)
return base.SetParametersAsync(ParameterView.Empty);
}

protected override bool ShouldRender()
{
// Re-rendering is required only if BindingContext is changed.
// If this method is not overridden, it re-renders all items in DataTemplateItemsComponent
// when new item is added there.
return _shouldRender;
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
if (_item != null)
Expand All @@ -59,8 +50,6 @@ protected override void BuildRenderTree(RenderTreeBuilder builder)
builder.AddAttribute(1, "ChildContent", Template.Invoke(_item));
builder.AddAttribute(2, nameof(RootContainerComponent.OnElementAdded), EventCallback.Factory.Create<object>(this, OnRootElementAdded));
builder.CloseComponent();

_shouldRender = false;
}
}

Expand All @@ -83,7 +72,6 @@ private void OnRootElementAdded(object rootControl)
if (newItem != null && !Equals(newItem, _item))
{
_item = newItem;
_shouldRender = true;
StateHasChanged();
}
};
Expand Down

0 comments on commit ae2b22e

Please sign in to comment.