Dispatching an event from an Action

Hi, should i dispatch an event from an action like this?
Action::make('prova')
->action(function () {
$this->dispatch('prova', ['prova']);
})
Action::make('prova')
->action(function () {
$this->dispatch('prova', ['prova']);
})
->dispatch('prova', ['prova']) doesn't work
15 Replies
krekas
krekas16mo ago
Yes
Patrick Boivin
Patrick Boivin16mo ago
@teodino93 What is the issue exactly? What's not working?
awcodes
awcodes16mo ago
Actions aren’t livewire components. Maybe
->action(function($livewire) {
$livewire->dispatch();
})
->action(function($livewire) {
$livewire->dispatch();
})
Also in lw3 dispatch events don’t use the array syntax for data. It should be named properties.
teodino93
teodino93OP16mo ago
If I write
Action::make('prova')->dispatch('prova')
Action::make('prova')->dispatch('prova')
Nothing happens, i have to do like the original post to dispatch an event... I don't know if it's an error or it's correct to do like this.
Patrick Boivin
Patrick Boivin16mo ago
Have you tried the suggestion from awcodes? 👆
teodino93
teodino93OP16mo ago
The code that i wrote on the original post works, I just wanted to be sure that should be the correct way to dispatch an event, since there is also a dispatch method.
LeandroFerreira
LeandroFerreira16mo ago
@awcodes , Sorry for tagging you, but do you know how we can dispatch events using the ->dispatch() method?
awcodes
awcodes16mo ago
Looking at the code it should work like you're thinking. Maybe it's a conflict in this case with the name of that action being the same as the event that is being dispatched. It might be trying to call itself.
LeandroFerreira
LeandroFerreira16mo ago
I've tried with different names
#[On('custom-event')]
public function getCustomActionEvent()
{
dd('custom action triggered');
}

//works
Action::make('custom_action1')
->action(fn () => $this->dispatch('custom-event')),

// does not work
Action::make('custom_action2')
->action(null)
->dispatch('custom-event')
#[On('custom-event')]
public function getCustomActionEvent()
{
dd('custom action triggered');
}

//works
Action::make('custom_action1')
->action(fn () => $this->dispatch('custom-event')),

// does not work
Action::make('custom_action2')
->action(null)
->dispatch('custom-event')
awcodes
awcodes16mo ago
With a null action, nothing happens on click anyway. Might have to ask Dan.
LeandroFerreira
LeandroFerreira16mo ago
yep, but without ->action(), ->action(null) this doesn't work
awcodes
awcodes16mo ago
Could even be a situation where using dispatch is only meant to be used internally in action callbacks since it is probably not part of the caching that happens with actions.
LeandroFerreira
LeandroFerreira16mo ago
hum, ok I don't think it's an issue, but I didn't find a way to execute this. Maybe we can check with Dan on how we can use this. Thank you for your support!
teodino93
teodino93OP16mo ago
Ok so we stick to action for now, thank you for the support!
KeyMe
KeyMe15mo ago
im following this thread too, on the surface it seems like action's dispatch() method doesn't work while livewire's does
Want results from more Discord servers?
Add your server