-
I'm not sure if this is a bug, or if I'm doing something wrong. My goal: when clicking a button, change the value of an <button
x-data="{ text: 'Click me' }"
@click="text = 'Clicked!'"
x-effect="setTimeout(() => text = 'Click me', 1000)"
x-text="text"
/> However, as you can see in this JSFiddle, the Am I missing something? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
x-effect will only track the variable if it's used directly in the expression. Because text is inside a callback within setTimeout, Alpine won't track the change. This version would work: <button
x-data="{ text: 'Click me' }"
@click="text = 'Clicked!'"
x-effect="text; setTimeout(() => text = 'Click me', 1000)"
x-text="text"
/> But... since In general it's not super robust, so you might just add the setTimeout in your click handler: <button
x-data="{ text: 'Click me' }"
@click="text = 'Clicked!'; setTimeout(() => text = 'Click me', 1000)"
x-text="text"
/> |
Beta Was this translation helpful? Give feedback.
x-effect will only track the variable if it's used directly in the expression. Because text is inside a callback within setTimeout, Alpine won't track the change. This version would work:
But...
since
text
is updated in a setTimeout callback, it's outside x-effect so it triggers the tracking again (another setTimeout) which means that, if you click the button again after it changes back, it will revert in less than 1 second.In general it's not super robust, so you might just add the setTimeout in your click handler: