Replies: 2 comments 3 replies
-
This is just a design issue. You aren't ever updating any reactive variables. also that is Alpine v2...that's over 2 years old. <div x-data="chars">
<div>
<span class="characterr1"
>Character >
<span
style="color: green"
x-show="chars[1]"
x-text="chars[1].name"
></span>
<span
style="color: orange"
x-show="!chars[1]"
>Offline</span
>
</span>
</div>
</div> Alpine.data('chars', () => ({
characters: [{ name: 'GreatFighter', online: false }],
get onlineCharacters() {
return this.characters.filter((c) => c.online);
},
init() {
setInterval(() => {
this.characters[0].online =
!this.characters[0].online;
}, 1000);
},
})); |
Beta Was this translation helpful? Give feedback.
-
Hello and thanks for the reply! Thank you for your suggestion. Sadly that's not something I can implement, I'm trying to use AlpineJS locally in an otherwise huge project made of raw JS + jQuery. I was hoping I could leverage reactivity in some way but it seems like I misunderstood, nevermind! |
Beta Was this translation helpful? Give feedback.
-
Hello!
I am trying to make my Alpine render more reactive. My
x-data
is based on an external variable (X.characters
) that will be updated by asetInterval
through some polling or websocket.My issue is that when a change occurs, the component doesn't take it into account as I thought it would.
I have created a simple reproduction on CodePen and below is the code to be future-proof.
The example changes the
online
property of my character every 1s to simulate receiving the information from the server.I would expect the printed info to alternate between
Offline
andGreatFighter
depending on whetheronline
istrue
orfalse
but it stays stuck in the initial configuration.HTML example
Beta Was this translation helpful? Give feedback.
All reactions