Trigger a refresh of a Volt main component from within a nested Livewire component
Hi!
I'm using a Volt componend with a nested Livewire component inside. I’d like to refresh (or re-render) the main Volt component when an action occurs in the nested component. What’s the best way to achieve this?
Section::make('deals')
->heading(false)
->schema([
Tabs::make('Tabs')
->tabs([
Tabs\Tab::make('Deals')
->badge(function (Project $record) {
return $record->countDeals();
})
->icon('phosphor-handshake-fill')
->schema([
\Filament\Infolists\Components\Livewire::make(DealsTable::class,
['project' => $this->project])->key('deals-table')
]),
In the volt component i use this code to update the livewire component:
$this->dispatch('refresh-data')->to(DealsTable::class);
#[On('refresh-data')]
But the other way around does not work...
I think that to refresh a Volt component from a nested Livewire component, I believe you should be able to dispatch an event from the nested component and listen for it in the Volt component:
So in your nested DealsTable component:
// Try dispatching an event to the parent
$this->dispatch('refresh-volt-component');
Then in your Volt component:
#[On('refresh-volt-component')]
public function refreshComponent()
{
// This should re-render the component
$this->render();
// Or you might need to refresh specific data:
// $this->project = $this->project->fresh();
}
Alternatively, you could try using $refresh
to trigger a complete component refresh:
#[On('refresh-volt-component')]
public function refreshComponent()
{
$this->js('$wire.$refresh()');
}
I think the key is that Volt components should listen for events the same way as regular Livewire components using the #[On('event-name')]
attribute. You might need to experiment with the exact syntax or check if there are any Volt-specific approaches.
Let me know how it goes!