FilamentF
Filamentβ€’3y ago
at92

Unable to dispatch/listen event from Action

I'm unable to dispatch and/or listen to a Livewire event from an Action. I have a resource with a table and a custom action that looks like this:

Action::make('switchTeam')
            ->label(__('Switch team'))
            ->icon('heroicon-s-arrow-right-circle')
            ->color('gray')
            ->action(function (Team $team) {
                // Something happens here
            })
            ->dispatch( 'switchedTeam' )


I then have a custom livewire component that I've loaded through render hooks inside the header bar next to the user's menu. In the component I've tried the following methods but nothing is being fired.

1st test:
protected $listeners = [
  'switchedTeam' => 'fireEvent',
];


2nd test:

#[On('switchedTeam')]
public function fireEvent() {
  Log::info('fireEvent');
}


But nothing is happening. What am I doing wrong? Thanks πŸ™
Solution
try this:
->action(function (Team $team) {
    //action here
})
->after(fn ($livewire) => $livewire->dispatch('switchedTeam'))


your component
use Livewire\Attributes\On;

#[On('switchedTeam')]
public function fireEvent()
{
    Log::info('fireEvent');
}
Was this page helpful?