Skip to content

Parameter updates not captured in SetParametersAsync if [Parameter]Changed assigns same value #64203

@yanisslav

Description

@yanisslav

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In .NET 10, we’ve noticed the following difference compared to .NET 8 and 9.

I’m not sure if this is actually a bug, but there is a clear difference - so this is more of a question to understand what the expected behavior should be.

We have the following:

InputComponent:

<input @bind="@Value" @oninput="OnInput" />

@code {
    private bool _valueChanged;

    [Parameter]
    public string Value { get; set; }

    [Parameter]
    public EventCallback<string> ValueChanged { get; set; }

    private async Task OnInput(ChangeEventArgs e)
    {
        Value = e.Value.ToString();
        await ValueChanged.InvokeAsync(Value);
    }

    public override Task SetParametersAsync(ParameterView parameters)
    {
        _valueChanged = HasParameterChanged(parameters, nameof(Value), Value);
        Console.WriteLine($"SetParametersAsync fired: {_valueChanged}");
        return base.SetParametersAsync(parameters);
    }

    public bool HasParameterChanged<TParameterType>(ParameterView parameters, string parameterName, TParameterType value)
    {
        if (parameters.TryGetValue(parameterName, out TParameterType parameterValue))
        {
            return !EqualityComparer<TParameterType>.Default.Equals(parameterValue, value);
        }

        return false;
    }
}

This works OK, except in cases like the following:

<InputComponent Value="@Value" ValueChanged="@OnValueChanged" />

@code {
    private string Value { get; set; } = "Initial Value";

    private void OnValueChanged(string newValue)
    {
        Value = "Initial Value";
    }
}

When typing in the input field, the ValueChanged event is triggered with the entered value, and the ValueChanged handler is invoked. In this handler, we set the Value parameter to the same value, instead of using the newValue.

Before invoking the ValueChanged event, we assign the Value of the InputComponent to the string value returned from the @oninput event.

Example (when entering “123” in the input):

.NET 8/9: Inside the SetParametersAsync method, we can detect that the Value set within the ValueChanged handler differs from the Value of the InputComponent.

Image

.NET 10: We can no longer detect this change - the SetParametersAsync is not triggered at all.

Image

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

10

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions