C
C#4h ago
Rese

@key directive switches two elements around for no apparent reason

Hello, Is there any scenario in which using the @key directive would cause two elements to switch around? From my understanding, when using foreach and @key within, the elements should retain the order they were in the UI before. I'm using Sortable JS to move the elements around and it works just fine. But the only issue arises when moving within a single foreach, if I move the element up by two spaces, it will appear normally, but on the next Blazor re-render, the top two elements will get switched around. This issue does not happen in any other case. So what happens: Initial ordering: 1 2 3 After move appears correctly: 3 1 2 Blazor re-renders: 1 3 2 If I reload page, it looks how it should: 3 1 2 Any ideas?
1 Reply
Rese
ReseOP4h ago
Or well to specify, this happens whenever moving an element up at least 2 spaces above, it will get switched around between the one newly moved and the one that's been there before.

Did you find this page helpful?