In one of my Vue components, I have implemented logic in the watch
prop that allows for sequential switching between elements when the down arrow key (key code 40) is pressed. While it is not too messy right now, there is a concern that it may become highly inefficient in the long run. Here is the current structure:
data () {
return {
firstElActive: true,
secondElActive: false,
thirdElActive: false
...
}
},
props: {
nextEl: {
type: Boolean,
default: false,
required: true
}
},
watch: {
nextEl: function (value) {
if (this.nextEl) {
if (this.firstElActive) {
this.firstElActive = !this.firstElActive;
this.secondElActive = !this.secondElActive;
... // other specific logic
}
else if (this.secondElActive) {
this.secondElActive = !this.secondElActive;
this.thirdElActive = !this.thirdElActive;
... // other specific logic
}
... // and so on
}
}
}
It's evident that this approach may lead to complications as the codebase grows. I have Lodash globally available (window._ = require('lodash')
) and wish to leverage it for refactoring. I'm currently exploring the most efficient methods to improve this. Any suggestions?