x-data within x-for loop — inconsistently updating #3372
-
Hi all — firstly, thanks for the great library! An odd issue I've noticed — I've scoured the docs for relevant answers here and I could have still missed the blindingly obvious, but this one is driving me a bit potty so thought I'd confirm with you lovely chaps to see if I'm going mad or not. Here's the JS fiddle — I've tried to whittle this down to the smallest example I can: https://jsfiddle.net/njpanderson/r86vxmqj/71/ Long and short is: When an Note: The nested From fiddling 🙄 with the jsfiddle above, it seems to only occur when an item with the same key ( Am I going mad? Is this expected, and/or is there something about this in the docs I'm simply missing? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
The issue has to do with how the parent scopes are propagated downwards to the children. Since the x-for isn't rerendering the looped item due to the key matching, the scope still contains the old For this contrived example, the fix is simple: have the replaced items have new keys (the id field). this should also be satisfactory for any real use case as you wouldn't realistically be replacing the array with items with the same keys but different data. If you did, then you need a more complex key to handle differentiation for the diffing algo. So I would say the behavior itself is exactly in spec with what someone aware of the technical implementation of alpine and how the keyed list works, but may not be obvious to people using it. I can't imagine there is a way to handle the situation you are describing (replacing an object in an array with another object that has the same key) and having it work. The real world use case would be to update the object if you're updating it, and having unique keys if its meant to be a different object entirely. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the answer @ekwoka — this seems to align with my initial thoughts on the matter — I had actually created a small function which essentially adds alpine specific keys to data arrays for the purposes of keeping them unique. However this doesn't feel sustainable.
Is this not a pretty typical use case for front-end data updated from a server side process? I could be getting the wrong end of the stick here!
I understand the concept here, but are you suggesting that if we're retrieving data from a server to update a list of items, that list needs to be updated entirely atomically? This is of course possible but feels like quite an important oversight in terms of documenting the management of data within an x-for loop. |
Beta Was this translation helpful? Give feedback.
The issue has to do with how the parent scopes are propagated downwards to the children.
Since the x-for isn't rerendering the looped item due to the key matching, the scope still contains the old
item
scope.For this contrived example, the fix is simple: have the replaced items have new keys (the id field).
this should also be satisfactory for any real use case as you wouldn't realistically be replacing the array with items with the same keys but different data.
If you did, then you need a more complex key to handle differentiation for the diffing algo.
So I would say the behavior itself is exactly in spec with what someone aware of the technical implementation of alpine and how the keye…