Skip to content
Discussion options

You must be logged in to vote

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 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:

  <button
    x…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Koniciuaaa
Comment options

@SimoTod
Comment options

SimoTod Feb 23, 2022
Collaborator

Answer selected by taobojlen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants