Skip to content

Duplicate @key values don't throw clear exception in if/else case #18314

@SteveSandersonMS

Description

@SteveSandersonMS

As reported here: #18307 (comment)

With this code,

@if (cat.IsBlack)
{
    <input @key="@cat.Id" type="checkbox"
        @onchange=@(eventArgs => { cat.IsBlack= (bool)eventArgs.Value; HandleCatColor(cat); })
        checked />
}
else
{
    <input @key="@cat.Id" type="checkbox"
    @onchange=@(eventArgs => { cat.IsBlack= (bool)eventArgs.Value; HandleCatColor(cat); }) />
}

This should throw a clear exception about reuse of the same @key value with a different sequence ID when toggling cat.IsBlack as per docs:

Ensure that values used for @key don't clash. If clashing values are detected within the same parent element, Blazor throws an exception

However because of the if/else, it doesn't throw but instead exhibits broken behavior.

This isn't super high priority because it's not a valid code pattern anyway. The issue is just that the error detection doesn't catch this particular case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Pillar: Dev Experienceaffected-very-fewThis issue impacts very few customersarea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.feature-razor.languagefeature-renderingFeatures dealing with how blazor renders componentshelp candidateIndicates that the issues may be a good fit for community to help with. Requires work from eng. teamseverity-nice-to-haveThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions