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?
DEV3 Developer Forum
Livewire 3 emit() no longer works. How do I send events between components?
Ask a question
Every post is reviewed first. Usually reviewed within 24 hours.
dev3 ·
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()becamedispatch()as well.- Namespaces:
App\Http\Livewireis nowApp\Livewire. wire:modelis 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