Alpine.js
$store
You can use $store to conveniently access global Alpine stores registered using Alpine.store(...) (covered in the next page). For example:
<button x-data @click="$store.darkMode.toggle()">Toggle Dark Mode</button>
...
<div x-data :class="$store.darkMode.on && 'bg-black'">
...
</div>
<script>
document.addEventListener('alpine:init', () => {
Alpine.store('darkMode', {
on: false,
toggle() {
this.on = ! this.on
}
})
})
</script>
$nextTick
$nextTick is a magic property that allows you to only execute a given expression AFTER Alpine has made its reactive DOM updates. This is useful for times you want to interact with the DOM state AFTER it's reflected any data updates you've made.
Alpine.store()
Alpine offers global state management through the Alpine.store() API, just like x-data for local state.
