Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Laravel

Livewire Morphs: Dynamic Component Switching

Dynamically switch between Livewire components with smooth transitions.

16 January 20261 min readLaravelاقرأ بالعربية

On this page

  1. Wire:Key for Morphing
  2. Dynamic Component
  3. Preserve State

Dynamic component switching:

Wire:Key for Morphing

blade
@foreach($tabs as $tab)
    <button wire:click="$set('activeTab', '{{ $tab }}')">
        {{ $tab }}
    </button>
@endforeach

<div wire:key="{{ $activeTab }}">
    @if($activeTab === 'profile')
        <livewire:profile-tab />
    @elseif($activeTab === 'settings')
        <livewire:settings-tab />
    @endif
</div>

Dynamic Component

blade
@livewire($componentName, ['id' => $id], key($id))

Preserve State

blade
<div wire:key="stable-key">
    <!-- Content preserves state -->
</div>
All articles

Keep reading

  • Laravel Action Classes Pattern16 January 20261 min read
  • Laravel Action Classes: Single Responsibility16 January 20261 min read
  • Laravel API Rate Limiting16 January 20261 min read