IgniteUI / igniteui-xplat-examples

MIT License
3 stars 1 forks source link

[Blazor] Refactor complicated code with the `BindElements` property #168

Open jsakamotoIGJP opened 1 year ago

jsakamotoIGJP commented 1 year ago

I found the complicated example code about the "IgbCategoryChart" below.

https://www.infragistics.com/products/ignite-ui-blazor/blazor/components/charts/types/area-chart

    private Action BindElements { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        var legend = this.legend;
        var chart = this.chart;

        this.BindElements = () =>
        {
            chart.Legend = this.legend;
        };
        this.BindElements();
    }

What that code above does is only assign the IgbLegend object to the Legend property of the IgbCategoryChart object. Regardless of that, the code is too complicated to easy understand.

That example code can be refactored like the one below. (Notice: The code below is taking care of the warning about nullable reference type)

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender && chart != null && legend != null)
        {
            chart.Legend = legend;
        }
    }

I've also found the same complicated examples that use the "BindElements" property on other pages. All of those complicated examples should be refactored.

gmurray81 commented 1 year ago

I'll have to look at what scenario we intended that to be used. Note IIRC we can't do it on first time since the refs aren't populated with that timing IIRC, but it's possible I'm thinking of react. But also the refs may change again later.