Blazor component: strange @attributes behavior #27932
-
Hello! Assume you have the
And the
The attributes dictionary always contains only one element, so I expect that result of the 'Next' button click should be:
But the actual result in is:
The issue occurs in a Blazor Server application with target framework .NET 5. Is it correct behavior? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
It's up to the receiving component's So in your logic, you keep supplying different boolean parameters, leaving other ones unspecified. So you accumulate more of them with value To avoid this, you could:
<Direction @attributes="attributes" />
<button class="btn btn-primary" @onclick="Next">Next</button>
@code {
Dictionary<string, object> attributes = new Dictionary<string, object>
{
{ nameof(Direction.Up), true },
{ nameof(Direction.Down), false },
{ nameof(Direction.Left), false },
{ nameof(Direction.Right), false },
};
void Next()
{
if ((bool)attributes[nameof(Direction.Up)])
{
attributes[nameof(Direction.Up)] = false;
attributes[nameof(Direction.Right)] = true;
}
else if ((bool)attributes[nameof(Direction.Right)])
{
attributes[nameof(Direction.Right)] = false;
attributes[nameof(Direction.Down)] = true;
}
else if ((bool)attributes[nameof(Direction.Down)])
{
attributes[nameof(Direction.Down)] = false;
attributes[nameof(Direction.Left)] = true;
}
else
{
attributes[nameof(Direction.Left)] = false;
attributes[nameof(Direction.Up)] = true;
}
}
}
|
Beta Was this translation helpful? Give feedback.
It's up to the receiving component's
SetParametersAsync
logic to choose how to handle receiving a partial set of parameters. The default behavior is additive. That is, for any parameters not specified, the default value is to leave the corresponding properties alone. It does not try to revert unspecified parameters to default values.So in your logic, you keep supplying different boolean parameters, leaving other ones unspecified. So you accumulate more of them with value
true
, and none of them get set back tofalse
.To avoid this, you could:
TestPage.razor
: