Introducing

How to Update Parent Livewire Component


Have you wonder how can you update the parent Livewire component?

Let's say you have to components a Comment component and Article component, something you find often in blog articles.

What we want is when the `Comment` component

is called after the user submits commit or delete one, the parent Livewire component in this case `Article` comment to show all newly received comments or count how many comments an article may have.

To achieve something like that we're going to use

Livewire Events to emitUp

event to the parent component or in this case Article component.

Go ahead open your Comment component and whatever you want to emit event up to parent component call $this->emitUp like

// Comment component
$this->emitUp('commentAdded', $comment);

Now in the parent Article component, listen for the event (which will receive the passed parameter):

// Article component
public $listeners = ['commentAdded'];

public function commentAdded(Comment $comment)
{
    // push newly received comment to comments list to be rendered by Laravel Blade
    // assuming $this->comments is an Eloquent Collection
    $this->comments->push($comment);
}
Categories:
Tags:
Livewire
Livewire event