Skip to content

Using the same @key value on two different elements fails in a strange way (attributes disappear) #47038

Open
@gdar91

Description

Update from @SteveSandersonMS: After discussion below, the original usage pattern was not supported, but the way it fails is strange and unexpected. Ideally we'd make this fail in a more understandable way.

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Whenever I use the @key attribute on a plain HTML element, it messes up its attribute values. It happens, when you have elements with the same keys in different conditional branches.
This is a simple example, based on the famous Counter component in the blazer template. I've modified it to display odd and even numbers in different colors and apply CSS transitions between them. So I want the rendering engine to preserve the element between redraws for CSS transitions to work.

<p role="status">
    Current count:
    @if (currentCount % 2 == 0)
    {
        <span @key='"count"' class="count even">@currentCount</span>
    }
    else
    {
        <span @key='"count"' class="count odd">@currentCount</span>
    }
</p>

Counter.razor.css

.count {
    transition: color ease 1s;
}

.count.even {
    color: royalblue;
}

.count.odd {
    color: orange;
}

Knowing the purpose of the @key attribute, I thought this code would work, but it doesn't. When I click the increment button, the odd cases work well, but the even cases, render the span without any attributes.

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

7.0.103

Anything else?

No response

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Pillar: Dev ExperiencePriority:3Work that is nice to havearea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions