Livewire always refreshes the component when the render method runs. So this should be unnecessary
Livewire V3 - how to run refresh or $refresh component from inside method
- Thanks for help *how to run refresh or $refresh component from inside method
- I'm using livewire 3
public function saved()
{
// code
$this->dispatch('refresh-the-component');
}
// in other component
#[On('refresh-the-component')]
public function refreshTheComponent()
{
// need to do Refresh this component after listen
}
@Snapey i do not know why it does not working without this it - also reactive attribute not working in child component
$this->dispatch('refresh-the-component');
in the component:
use Livewire\Attributes\On;
#[On('refresh-the-component')]
class SomeComponent extends Component { }
just writing #[On("SomeEvent")] refreshes the component without any need for a function
usually with only #on on the compoennt call render again, but if not, you can create a variable like public $forceRefresh = 0; and in your #on method, increase the forcerefresh +1, idk why, sometimes just dont re-render so this trick works
hello respect, i had this issue today & managed to solve it by inspecting my code. now i share my resolution hope it will help others. notice livewire only re-renders a component if a state(public property) changes, just triggering an action is not enough, because livewire does a lot of checks behind the scene to prevent redundant rendering. so if you trigger an action 100 times but each time set a property to 0 for example, component re-renders only the first time(if property is not 0 beforehand) in case of cart component i guess the way to go is cookie. notice cookie is something kept in user browser & won't update until you Cookie::queue() which sends the new cookie with the next response. so if you read cookie after queuing it because it is still old value, livewire will not re-render component because the data is the same with what is set to when mounted.
hope it helps
Hi-
I had this issue, too. I wanted to hide charts showing on a page when a child component (the toggle) was set to "Hide Inline Charts" - The child component is a tiny LW component that just renders the button and sets a user pref.
To make this all work, I added this mini function to accept the dispatch from the child component:
// Magic method to handle the hideInlineCharts event
// https://livewire.laravel.com/docs/events#listening-for-events-from-specific-child-components
// https://laracasts.com/discuss/channels/livewire/livewire-v3-how-to-run-refresh-or-refresh-component-from-inside-method
#[On('hideInlineCharts')]
public function hideInlineCharts(): void
{
return;
}
In the child button that toggles the Show/Hide charts behavior, the blade include for the PARENT component's bladed looks like this:
<livewire:button-hide-inline-charts />
NOTE: The @hideInlineCharts="$refresh" didn't do it for me - I am sure it was because I was doing something wrong?? We're Livewire 3.0 on Laravel 11.x as of this writing.
This is the simplified blade for the ButtonHideInlineCharts Livewire component:
<flux:switch
x-on:click="removeFocus();"
wire:click="setHideInlineChartsPref"
wire:model.live="hideInlineCharts"
label="Hide Class Profile Charts" />
NOTE: We're using a flux switch/toggle, but you could use a WireUI or other Livewire-friendly switch/toggle.
This is the simplified setHideInlineChartsPref method that's being called by wire:click:
public function setHideInlineChartsPref(): void
{
$this->hideInlineCharts = ! $this->hideInlineCharts;
$this->dispatch('hideInlineCharts');
}
The "magic" is the $this->dispatch('hideInlineCharts'); which is "heard" by the dummy method/function in the parent, e.g., from above:
#[On('hideInlineCharts')]
public function hideInlineCharts(): void
{
return;
}
This was dead simple and provided lovely reactive behavior on the parent page that had deeply nested LW components itself!
Trying to pay back all those who have provided solutions in these forums that have helped me in the past. I hope this helps the next person that comes looking like I had!
Please or to participate in this conversation.