dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.19k stars 9.93k forks source link

question: Bound parameter value #16070

Closed dlr1 closed 4 years ago

dlr1 commented 6 years ago

I have a component("mycomp"). I am just writing the value of parameter and expect the values to be in sync. what am I missing?

<div class="row" style="height:100px;width:300px;">
    bound charttype value:@ChartType
    <br />
    ChartTypeInMethod: @ChartTypeInMethod
</div>

@functions{
    [Parameter]
    string ChartType { get; set; }

    string ChartTypeInMethod { get; set; }
    public void MyMethod()
    {
        ChartTypeInMethod = ChartType;
    }
}

I am using the above component in another page like below

<div class="row">
    <mycomp ChartType="@ChartType" ref="comp1"></mycomp>   
    <div>
        <input type="radio" name="charttype" checked="@(ChartType=="bar")" value="bar" onchange="@((e)=>charttypeChanged(e))"/>bar
        <input type="radio" name="charttype" checked="@(ChartType=="line")" value="line" onchange="@((e)=>charttypeChanged(e))" />line
    </div>
</div>

@functions{        
    string ChartType { get; set; }
    mycomp comp1;

    void charttypeChanged(UIChangeEventArgs e)
    {
        ChartType = e.Value.ToString();        
        comp1.MyMethod();
    }
}
SteveSandersonMS commented 6 years ago

Please see Component Attributes on this page for information on the supported syntax: https://blazor.net/docs/components/index.html

Also the following post might be helpful: https://github.com/aspnet/Blazor/issues/743#issuecomment-386548638

dlr1 commented 6 years ago

Just to clarify, I am not talking about the values not being updated.

The scenario is.

  1. I click the radiobutton say "bar", onchange will be called ChartType will be updated the parameter in the component is updated. MyMethod of the component is called, there I set another variable value to the parameter value. I expect both of them to be same.

The Gist of what I am trying to do is, have a bunch of parameters set on a component and then call a method in the component to draw the chart. what I see happening is even though the parameter value in the component is updated, the value of that in the method doesn't reflect the same value.

SteveSandersonMS commented 6 years ago

Blazor doesn't know you've changed the state inside mycomp unless it calls StateHasChanged.

dlr1 commented 6 years ago

StateHasChanged has no effect

SteveSandersonMS commented 6 years ago

Oh I see. You're calling MyMethod before the component has rendered, and hence it has not yet received an updated [Parameter] value.

These kinds of ordering dependencies are one of the reasons why it's recommended to use declarative methods for passing data, rather than procedural ones via ref and calling methods. Where possible, don't call methods on child components. Just update the parameters you're passing to them. The child component can react to updated incoming values by overriding OnParametersSet/OnParametersSetAsync if you need.

dlr1 commented 6 years ago

It works if I use OnParametersSet, I assumed when I change the parameter value it is reflected immediately. I understand now. Thanks