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

Parent-Child Communication in Livewire v4

Pass data and events between nested components.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Props - Passing Data Down
  2. Events - Dispatching Up
  3. Two-Way Binding with wire:model

Livewire v4 provides multiple ways for component communication.

Props - Passing Data Down

php
// Parent Component View
<livewire:child-component 
    :user="$user"
    :posts="$posts"
    title="Dashboard"
/>

// Child Component
class ChildComponent extends Component
{
    public User $user;
    public Collection $posts;
    public string $title;
    
    public function mount(User $user, Collection $posts, string $title): void
    {
        $this->user = $user;
        $this->posts = $posts;
        $this->title = $title;
    }
}

Events - Dispatching Up

php
// Child Component - dispatch event to parent
class ItemSelector extends Component
{
    public function selectItem(int $id): void
    {
        $this->dispatch('item-selected', id: $id, name: 'Product');
    }
}

// Parent Component - listen for event
class ParentComponent extends Component
{
    public ?int $selectedItemId = null;
    
    #[On('item-selected')]
    public function handleSelection(int $id, string $name): void
    {
        $this->selectedItemId = $id;
        session()->flash('message', "Selected: {$name}");
    }
}

Two-Way Binding with wire:model

php
// Parent using wire:model
<livewire:color-picker wire:model="selectedColor" />
<livewire:date-picker wire:model.live="startDate" />

// Child Component (ColorPicker)
class ColorPicker extends Component
{
    #[Modelable]
    public string $value = '#000000';
    
    public function render()
    {
        return view('livewire.color-picker');
    }
}
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read