You can hook into the updated()
method of your component lifecycle to have your code execute on update. And not on first render:
It might be a bit more difficult as you’re wanting to execute JS on refresh. You may want to use $this->dispatchBrowserEvent('js-event')
to trigger your JS code. I tried to find the docs on this method but I don’t think it’s in the docs yet. Basically, it will call the js-event
and if you have something in javascript land listening for that event, it can run a method.
I’m just typing out the following from my head and if I have some time a little later, I’ll test to make sure it works but to get you started:
Component.php
public function updated()
{
$this->dispatchBrowserEvent('js-event');
}
component.blade.php
<div>
<div x-data="appData()" x-on:js-event.window="jsMethod()">
<!-- HTML -->
<livewire:child-component />
</div>
</div>
<script>
function appData()
{
return {
jsMethod() {
// your code
}
}
}
This is Alpine but you can use whatever JS you’re comfortable with. x-on
is listening for js-event
on window
and will trigger jsMethod()
when the event is raised.
Your child component shouldn’t kick up anything that will trigger a refresh on the parent component.