packages/docs/src/en/essentials/lifecycle.md
Alpine has a handful of different techniques for hooking into different parts of its lifecycle. Let's go through the most useful ones to familiarize yourself with:
<a name="element-initialization"></a>
Another extremely useful lifecycle hook in Alpine is the x-init directive.
x-init can be added to any element on a page and will execute any JavaScript you call inside it when Alpine begins initializing that element.
<button x-init="console.log('Im initing')">
In addition to the directive, Alpine will automatically call any init() methods stored on a data object. For example:
Alpine.data('dropdown', () => ({
init() {
// I get called before the element using this data initializes.
}
}))
<a name="after-a-state-change"></a>
Alpine allows you to execute code when a piece of data (state) changes. It offers two different APIs for such a task: $watch and x-effect.
<a name="watch"></a>
$watch<div x-data="{ open: false }" x-init="$watch('open', value => console.log(value))">
As you can see above, $watch allows you to hook into data changes using a dot-notation key. When that piece of data changes, Alpine will call the passed callback and pass it the new value. along with the old value before the change.
<a name="x-effect"></a>
x-effectx-effect uses the same mechanism under the hood as $watch but has very different usage.
Instead of specifying which data key you wish to watch, x-effect will call the provided code and intelligently look for any Alpine data used within it. Now when one of those pieces of data changes, the x-effect expression will be re-run.
Here's the same bit of code from the $watch example rewritten using x-effect:
<div x-data="{ open: false }" x-effect="console.log(open)">
Now, this expression will be called right away, and re-called every time open is updated.
The two main behavioral differences with this approach are:
$watch is "lazy" -- won't run until the first data change)$watch receives both the new value AND the old one)<a name="alpine-initialization"></a>
<a name="alpine-initializing"></a>
alpine:initEnsuring a bit of code executes after Alpine is loaded, but BEFORE it initializes itself on the page is a necessary task.
This hook allows you to register custom data, directives, magics, etc. before Alpine does its thing on a page.
You can hook into this point in the lifecycle by listening for an event that Alpine dispatches called: alpine:init
document.addEventListener('alpine:init', () => {
Alpine.data(...)
})
<a name="alpine-initialized"></a>
alpine:initializedAlpine also offers a hook that you can use to execute code AFTER it's done initializing called alpine:initialized:
document.addEventListener('alpine:initialized', () => {
//
})