What's the correct way to remove and add nodes in one pass? #1225
-
Scenario I've mutated some state and then I am replacing the nodes with a newly constructed set of nodes Problem I've created a reproduction (link below). But the gist of it is suppose I have a function like this: const elements = ref([])
let pass = 0
function generateNodes() {
while (elements.value.length > 0) {
console.log(`Popped`)
elements.value.pop()
}
console.log('Next nodes...')
for (let i = 0; i < 4; i++) {
elements.value.push({
id: `${i}_${pass}`,
label: `Node: ${i} @ ${pass}`,
position: {
x: 0,
y: (i + pass * 4) * 40,
},
})
}
pass++
console.log(`Completed pass ${pass} with ${elements.value.length} nodes`)
} When Is there a correct way to perform this type of mutation where I'm replacing a set of nodes with another set of nodes? If I only pop 1, it works as expected: function generateNodes() {
/*
while (elements.value.length > 0) {
console.log(`Popped`)
elements.value.pop()
}
*/
elements.value.pop()
} Reproduction https://codesandbox.io/p/devbox/wonderful-lehmann-wljgw5?file=%2Fsrc%2FApp.vue |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
What does this mean? Can you elaborate? Honestly, just overwrite your original ref with a new array instead of mutating it and you should already be good to go 😄 |
Beta Was this translation helpful? Give feedback.
What does this mean? Can you elaborate?
Honestly, just overwrite your original ref with a new array instead of mutating it and you should already be good to go 😄