Skip to content
Gx
GitHub

GX2012: signal instance needs a key

A component with signals renders in a loop and has no key.

Cause

A component with signals renders in a loop and has no key. Each instance needs a key so that it has its own signals.

Example

cart/Counter.gx
package cart

signals {
  Count int = 0
}

<button on:click={$Count++}>Add</button>
cart/List.gx
package cart

props {
  Names []string
}

<ul>
  for range p.Names {
    <li><Counter /></li>
  }
</ul>

gx check reports:

gx check
cart/List.gx:9:9: GX2012: component <Counter> has signals and renders in a loop; add key={expr} so each instance keeps its own signals

Fix

Add key={expr} to the component call or to the element around it. Use a value that is unique for each instance.