I am reading the documentation right now. It's bizarre that you seemingly are not following the instructions. It's not $dispatchTo, it's $emitTo, and the parameters are not right. Pls work according to the docs.
Help Needed with Livewire Event Dispatching in Livewire 3
Hello everyone,
I am currently facing an issue with event dispatching in Livewire 3 and would appreciate any assistance or insights you might offer. I'm trying to dispatch an event from a Blade to a Livewire component, but it seems the event is not being triggered or received by the target component.
In a Livewire component I have the replays for a thread. each reply has a edit button
<button wire:click="$dispatchTo('EditReply', 'setEditReply', { id: {{ $reply->id }} })"
>Edit Replay</button>
And this is the listener in my Livewire component (EditReply):
<?php
class EditReply extends Component
{
public Reply $reply;
protected $listeners = [
'setEditReply'
];
public function setEditReply($data)
{
$replyId = $data['id'];
dd($replyId);
$this->reply = Reply::findOrFail($replyId);
}
public function render(): View|Application|Factory|\Illuminate\Contracts\Foundation\Application
{
return view('livewire.forum.edit-reply');
}
}
However, it seems that the setEditReply method in the EditReply component is not being called. I expected to see the output from dd($replyId) in the setEditReply method, but there's no response when the button is clicked.
Your dispatchto should be the javascript name of the component not the PHP name so probably
<button wire:click="$dispatchTo('edit-reply', 'setEditReply', { id: {{ $reply->id }} })"
Also note that above will only work if your id is an integer. Any other type such as uuid will need quoting.
Please or to participate in this conversation.