Skip to content
Gx
GitHub

GX2009: loop needs a key

A loop body holds an input, a component with signals or a transition, and it has no key.

Cause

A loop body holds an input, a component with signals or a transition, and it has no key. Without a key, an update can move state from one row to a different row.

Example

cart/List.gx
package cart

props {
  Names []string
}

<ul>
  for _, name := range p.Names {
    <li><input value={name} /></li>
  }
</ul>

gx check reports:

gx check
cart/List.gx:8:3: GX2009: loop needs a key: add key={expr} or a #fragment with a key parameter

Fix

  1. Add key={expr} to the first element of the loop body. Use a value that is unique for each row.
  2. Or mark the element as a fragment with a key parameter.