Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I integrate JavaScript hooks with Livewire lifecycle hooks


How do I integrate JavaScript hooks with Livewire lifecycle hooks


To integrate JavaScript hooks with Livewire's lifecycle hooks, you can follow these steps:

1. Declare the JavaScript hooks in your Blade template:

html
<script>
document.addEventListener('livewire:load', () => {
    window.livewire.hook('component.initialized', (component) => {
        // Runs when the component is initialized
    });

    window.livewire.hook('element.initialized', (el, component) => {
        // Runs when an element is initialized
    });

    // Add other JavaScript hooks as needed
});
</script>

2. Use Livewire's PHP lifecycle hooks to trigger your JavaScript hooks:

php
class MyComponent extends Component
{
    public function mount()
    {
        // Runs when the component is first created
        $this->dispatchBrowserEvent('component.initialized');
    }

    public function hydrate()
    {
        // Runs when the component is hydrated on subsequent requests
        $this->dispatchBrowserEvent('component.hydrated');
    }

    public function updated($name, $value)
    {
        // Runs after a property is updated
        $this->dispatchBrowserEvent('component.updated', [
            'name' => $name,
            'value' => $value,
        ]);
    }

    public function render()
    {
        // Runs before the component is rendered
        $this->dispatchBrowserEvent('component.rendering');

        return view('my-component');
    }
}

3. Handle the dispatched events in your JavaScript hooks:

javascript
document.addEventListener('livewire:load', () => {
    window.livewire.hook('component.initialized', (component) => {
        // Handle the component.initialized event
    });

    window.livewire.hook('component.hydrated', (component) => {
        // Handle the component.hydrated event
    });

    window.livewire.hook('component.updated', (data) => {
        // Handle the component.updated event
        console.log(`The "${data.name}" property was updated to "${data.value}".`);
    });

    window.livewire.hook('component.rendering', (component) => {
        // Handle the component.rendering event
    });
});

By combining Livewire's PHP lifecycle hooks and the corresponding JavaScript hooks, you can integrate your custom JavaScript logic with the different stages of a Livewire component's lifecycle. This allows you to perform additional actions, such as initializing third-party libraries, updating the DOM, or logging events, at specific points in the component's lifecycle.

Remember to use the `dispatchBrowserEvent()` method in your Livewire component to trigger the corresponding JavaScript hooks, and then handle those events in your JavaScript code.

Citations:
[1] https://laravel-livewire.com/docs/2.x/lifecycle-hooks
[2] https://livewire.laravel.com/docs/lifecycle-hooks
[3] https://laravel-livewire.com/docs/1.x/lifecycle-hooks
[4] https://www.youtube.com/watch?v=QADUTtkH68Q
[5] https://laravel-livewire.com/docs/2.x/reference