depends how you are calling/triggering this addRuleToSection method, instead of console logs you can add breakpoints break; and follow step by step
Mar 7, 2024
5
Level 9
Why does this create a Loop
I'm trying to add/remove an item from a reactive array but I'm not sure why this code is creating an infinite loop where the gets added and removed and added and removed and ...... forever.
This is the code on my page
props: {
section: {
type: Object,
required: true,
default: () => ({
key: uuid(),
title: null,
number: null,
survey_section_rules: [],
survey_questions: []
})
},
},
methods: {
addRuleToSection(rule_id) {
console.log('start')
const index = this.section.survey_section_rules.indexOf(rule_id);
if (index === -1) {
console.log('add')
this.section.survey_section_rules.push(rule_id);
} else {
if (rule_id === this.section_rule_types.find(x => x.name === 'is_repeated').id) {
console.log('remove is_repeated')
this.section.survey_section_rules = [];
} else {
console.log('remove other')
this.section.survey_section_rules.slice(index, 1);
}
}
console.log('done')
},
},
the console.log looks like this
start
add
done
start
remove is_repeated
done
start ...
Why does this create a loop and how can I create a code that does this correctly?
Please or to participate in this conversation.