x-for trying to loop through a cleared object's keys #4231
-
Hi there, Having a really weird behaviour with one of my x-for loops here, wondering if someone can point me to what I'm missing.
And then in the wrapper of this multi-selector I have an event listener (the event is submitted by the livewire class when the form submission is successful), which clears that selected object:
The problem is that after the object is cleared, the template is still trying to loop through the selected keys (no idea through what keys), and it returns console errors triggered in the body of the for loop: Cannot read properties of undefined (reading 'selected') Tried to add an x-show checking whether the object has keys or not, but that didn't fix it. Question: how that loop is generated if the object is empty and has no keys? Probably adding some ternary operators would avoid the errors to be thrown, but still would like to figure out, why the body of that loop is reached. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Easy, just stop looping over keys. Loop over entries. <template x-for="[id, value] in Object.entries(selected)" :key="id"> In your above, your effects are using the key to access selected, but if selected changes, the key won't point to anything anymore, but the effect may still try to run before being cleaned up. |
Beta Was this translation helpful? Give feedback.
Easy, just stop looping over keys.
Loop over entries.
In your above, your effects are using the key to access selected, but if selected changes, the key won't point to anything anymore, but the effect may still try to run before being cleaned up.