DEV3 Technologies home

DEV3 Developer Forum

Livewire 3 emit() no longer works. How do I send events between components?

Laravel helpSolved0 views
dev3 ·

After upgrading to Livewire 3 I get 'Method emit does not exist' when calling $this->emit('postSaved'). How do I communicate between components now, and how do I listen to the event?

#laravel#livewire

0

1 reply

dev3 · Accepted answer

emit, emitTo, emitUp and emitSelf were removed in Livewire 3. Everything uses dispatch() now, and events are real browser events.

Dispatching

$this->dispatch('post-saved', id: $post->id);          // to all components and JS
$this->dispatch('post-saved', id: $post->id)->to(PostList::class);   // one component
$this->dispatch('post-saved')->self();                 // this component only

Named arguments become keys in the payload.

Listening in PHP with the On attribute:

use Livewire\Attributes\On;

#[On('post-saved')]
public function refreshList(int $id): void
{
    // reload data
}

You can still use the $listeners property for dynamic names, but the attribute is preferred.

Listening in Blade or Alpine / JavaScript

<div x-on:post-saved.window="alert('Saved ' + $event.detail.id)"></div>

Dispatch from the frontend with $dispatch('post-saved', { id: 5 }) and in a view wire:click="$dispatch('post-saved')".

Other renamed things during the v2 to v3 upgrade

  • $this->dispatchBrowserEvent() became dispatch() as well.
  • Namespaces: App\Http\Livewire is now App\Livewire.
  • wire:model is deferred by default (use .live).
  • The layout default is components.layouts.app.

Run through the official upgrade guide and search your project for emit( and dispatchBrowserEvent( to find all call sites.

0

Your reply

Every post is reviewed by DEV3 before it appears. Usually reviewed within 24 hours.

Markdown supported. Use ``` for code blocks. Images are not allowed.