Why is the @key directive critical when rendering lists of components or elements, and what is its role in the render tree diffing process?

Last updated 5 July 2026

  • Without @key, the diff matches list items by position, so inserts, removes, or reorders can attach component state, focus, or input values to the wrong row.

  • With @key, items are matched by key: moved items are moved (not rebuilt), removed components are disposed correctly, and per-item state is preserved.

  • Use a stable unique value like an entity Id, never the loop index.

  • https://learn.microsoft.com/en-us/aspnet/core/blazor/components/element-component-model-relationships?view=aspnetcore-10.0
  • https://blazor-university.com/components/render-trees/optimising-using-key/
  • https://www.meziantou.net/optimizing-blazor-performance-using-the-key-directive.htm
  • https://learn.microsoft.com/en-us/aspnet/core/blazor/components/templated-components?view=aspnetcore-10.0
  • https://isitvritra101.medium.com/blazors-key-attribute-a-practical-guide-411a592439c4
All Blazor interview questions