Home / Snippets / How to Watch Alpine.js Store Value
How to Watch Alpine.js Store Value cover

How to Watch Alpine.js Store Value

4.4K

2 years ago

0 comments

In an Alpine.js store, you can make use of "Alpine.effect()" in order to watch the property. With the code example below, anytime the "name" property updates, the value will be console logged to the console devtool.
Alpine.store("alpineStore", {
    name: 'Hi Dad!',
    
    init() {
        Alpine.effect(() => {
            console.log(this.name);
        });
    },
});
Another way to watch an Alpine.js store property you can make use of the x-init directly on the alpine instance itself.
<div x-data x-init="$watch('$store.alpineStore.name', function (val) {
    console.log(val);
})"></div>
Now, anytime the "name" property is updated, the value of the name will be console logged to the console tab of the devtools.
I hope this tutorial is beneficial to you and if you do find it helpful do make sure to share it with your friends. Cheers and happy coding!
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this