Root div has wire:key, so ALL nested DOM elements preserve their state correctly during reordering!
class ComplexTodoList extends Component
{
public $todos = [];
#[On('toggle-todo')]
public function toggleTodo($todoId) { /* handles child events */ }
#[On('move-up')]
public function moveUp($todoId) { /* reorder logic */ }
}
class TodoItem extends Component
{
public $todo, $index, $totalCount;
public function toggleCompleted() {
$this->dispatch('toggle-todo', $this->todo['id']);
}
}
@foreach($todos as $index => $todo)
<!-- Root container div WITH wire:key -->
<div wire:key="todo-item-{{ $todo['id'] }}" class="bg-gradient-to-r from-purple-50 to-blue-50 p-2 rounded-lg">
<!-- Inner wrapper div without wire:key -->
<div class="bg-white rounded border p-1">
<!-- Nested Livewire component with wire:key -->
<livewire:todo-item
:todo="$todo"
:index="$index"
:totalCount="count($todos)"
wire:key="todo-item-{{ $todo['id'] }}"
/>
</div>
<!-- Additional nested div without wire:key -->
<div class="mt-2 px-2 py-1 bg-purple-50">
<span>ID: {{ $todo['id'] }}</span> |
<input type="text" placeholder="State preserved!" class="ml-2" />
</div>
<!-- Another div without wire:key -->
<div class="mt-1 px-2 py-1 bg-blue-50">
<input type="checkbox" />
<label>Check me - state preserved!</label>
</div>
</div>
@endforeach
CORRECT: Root div has wire:key with stable todo ID
CORRECT: Livewire component has wire:key with stable todo ID
CORRECT: All nested divs inherit proper DOM tracking from root div