Events
- Firing Events
- Event Listeners
- Passing Parameters
- Scoping Events
- Listening For Events In JavaScript
- Dispatching Browser Events
Livewire components can communicate with each other through a global event system. As long as two Livewire components are living on the same page, they can communicate using events and listeners.
Firing Events
There are multiple ways to fire events from Livewire components.
Method A: From The Template
1<button wire:click="$emit('postAdded')">
Method B: From The Component
1$this->emit('postAdded');
Method C: From Global JavaScript
1<script>2 window.livewire.emit('postAdded')3</script>
Event Listeners
Event listeners are registered in the $listeners
property of your Livewire components.
Listeners are a key->value pair where the key is the event to listen for, and the value is the method to call on the component.
1use Livewire\Component; 2 3class ShowPosts extends Component 4{ 5 public $addedMessageVisible = false; 6 7 protected $listeners = ['postAdded' => 'showPostAddedMessage']; 8 9 public function showPostAddedMessage()10 {11 $this->addedMessageVisible = true;12 }13 14 public function render()15 {16 return view('livewire.show-posts');17 }18}
Now when any other component on the page emits a postAdded
event, this component will pick it up and fire the showPostAddedMessage
method on itself.
protected $listeners = ['postAdded'];
will call the postAdded
method when the postAdded
event is emitted.
If you need to name event listeners dynamically, you can substitute the $listeners
property for the getListeners()
protected method on the component:
1use Livewire\Component; 2 3class ShowPosts extends Component 4{ 5 public $addedMessageVisible = false; 6 7 protected function getListeners() 8 { 9 return ['postAdded' => 'showPostAddedMessage'];10 }11 12 ...13}
Passing Parameters
You can also send parameters with an event emission.
1$this->emit('postAdded', $post->id);
1use Livewire\Component; 2 3class ShowPosts extends Component 4{ 5 public $addedMessageVisible = false; 6 public $addedPost; 7 8 protected $listeners = ['postAdded']; 9 10 public function postAdded($postId)11 {12 $this->addedMessageVisible = true;13 $this->addedPost = Post::find($postId);14 }15 16 public function render()17 {18 return view('livewire.show-posts');19 }20}
Scoping Events
Scoping To Parent Listeners
When dealing with nested components, sometimes you may only want to emit events to parents and not children or sibling components.
In these cases, you can use the emitUp
feature:
1$this->emitUp('postAdded');
1<button wire:click="$emitUp('postAdded')">
Scoping To Components By Name
Sometimes you may only want to emit an event to other components of the same type.
In these cases, you can use emitTo
:
1$this->emitTo('counter', 'postAdded');
1<button wire:click="$emitTo('counter', 'postAdded')">
(Now, if the button is clicked, the "postAdded" event will only be emitted to counter
components)
Scoping To Self
Sometimes you may only want to emit an event on the component that fired the event.
In these cases, you can use emitSelf
:
1$this->emitSelf('postAdded');
1<button wire:click="$emitSelf('postAdded')">
(Now, if the button is clicked, the "postAdded" event will only be emitted to the instance of the component that it was emitted from.)
Listening For Events In JavaScript
Livewire allows you to register event listeners in JavaScript like so:
1<script>2document.addEventListener('livewire:load', () => {3 window.livewire.on('postAdded', postId => {4 alert('A post was added with the id of: ' + postId);5 })6});7</script>
Dispatching Browser Events
Livewire allows you to fire browser window events like so:
1$this->dispatchBrowserEvent('name-updated', ['newName' => $value]);
You are able to listen for this window event with JavaScript:
1<script>2window.addEventListener('name-updated', event => {3 alert('Name updated to: ' + event.detail.newName);4})5</script>
AlpineJS allows you to easily listen for these window events within your HTML:
1<div x-data="{ open: false }" @name-updated.window="open = false">2 <!-- Modal with a Livewire name update form -->3</div>