Livewire 1.3
I encountered a particular scenario that I’m just looking for clarification on to satisfy my curiosity.
I have two Livewire components that sometimes communicate through events. I noticed that a scenario when if I fire an emit event on one from inside an if-conditional, it doesn’t reach the other despite the statement being true and sibling code being successfully executed. If I move the emit outside of the block, it fires.
A more basic example looks a bit like this:
Component A
public function getComments($target_id)
{
$this->target['id'] = $target_id;
if($this->comments = Comment::model_comments($this->target)->toArray())
{
$this->dispatchBrowserEvent('commentsRetrieved');
}
}
public function submit()
{
...some code that validates, creates and returns created comment model
// fire livewire event: THIS FIRES
$this->emit('commentAdded', $this->new_comment['commentable_id']);
// reload comments
if($this->getComments($this->new_comment['commentable_id']))
{
// fire browser event: reload comments
$this->dispatchBrowserEvent('commentsRetrieved');
// fire livewire event: THIS DOESN'T
$this->emit('commentAdded', $this->new_comment['commentable_id']);
}
}
(I’m not trying to fire both emits, just included both for illustration purposes)
Component B
protected $listeners = ['commentAdded' => 'incrementCount'];
public function incrementCount($target_id)
{
if($this->target_id === $target_id)
{
$this->count = ($this->count + 1);
}
}
It’s not an issue for me atm but I’m curious as to why this is the case so I know going forward.