Without wire:key on the root div, Livewire components lose their connection to the backend. Button clicks fail with console errors, and the DOM state gets scrambled!
@foreach($todos as $index => $todo)
<div class="bg-gradient-to-r from-yellow-50 to-orange-50 p-2 rounded-lg">
<div class="bg-white rounded border p-1">
<livewire:todo-item
:todo="$todo"
:index="$index"
:totalCount="count($todos)"
wire:key="todo-item-{{ $todo['id'] }}"
/>
</div>
<div class="mt-2 px-2 py-1 bg-yellow-50">
<span>ID: {{ $todo['id'] }}</span> |
<input
type="text"
placeholder="This input will lose state!"
class="ml-2 border border-yellow-400"
/>
</div>
<div class="mt-1 px-2 py-1 bg-orange-50">
<input type="checkbox" class="ml-2" />
<label>Check me - this state will be lost!</label>
</div>
</div>
@endforeach
INSUFFICIENT: Livewire component has wire:key but root div doesn't
Having wire:key only on the Livewire component is NOT enough! Without wire:key on the root div of the @foreach loop, Livewire completely breaks. The root div wire:key is absolutely essential.