Spruce.watch() alternative? #1653
-
How do I do something like this in Alpine v3? Object.keys(defaultSettings).forEach((key, index) => {
Spruce.watch(`settings.${key}`, (next) => {
setQueryStringParameter(key, encodeURIComponent(JSON.stringify(next)));
});
}); |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
There are a few ways but not as clean/strait forward as it was in Spruce. This is a method I’m using in some stores just to keep the code sort of in same place it was with Spruce:
And in component that ties into this store I just do something like You could also move that watch into a component but I like it with store and just pass watch magic to it which is just doing Alpine.effect(()=>{}) with store. This shows alternative strategy using Alpine.effect (I’ve been lazy and have not tried it yet): #1506 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
Like @danddanddand is saying you can use Alpine.effect(() => {
Object.keys(defaultSettings).forEach((key, index) => {
let value = Alpine.store('settings')[key]
setQueryStringParameter(key, encodeURIComponent(JSON.stringify(value)));
});
}) |
Beta Was this translation helpful? Give feedback.
Like @danddanddand is saying you can use
Alpine.effect
instead of a manual watcher: