I have a computed()
property with the following console.log within it:
props: {
productGroup: {
type: Object,
default: {},
},
filterState: {
type: Object,
default: {},
}
},
computed: {
filterProducts() {
console.log(this.productGroup.Polarized);
// outputs --> "{standard: {mirrored:TRUE: Array(8), mirrored:FALSE: Array(5)}}"
console.log(this.filterState.size);
// outputs --> "standard"
console.log(this.productGroup.Polarized, this.productGroup.Polarized[this.filterState.size], this.filterState.size);
// outputs "{standard: {mirrored:TRUE: Array(8), mirrored:FALSE: Array(5)}}",
"undefined", <------ why is that??
"standard"
if (!this.productGroup.Polarized[this.filterState.size] &&
!this.productGroup['Non-Polarized'][this.filterState.size] &&
!this.productGroup['Elite Polarized'][this.filterState.size]) {
return false;
}
...
...
return ...
}
My question is why did this.productGroup.Polarized[this.filterState.size] return undefined when this.productGroup.Polarized is updated with the key(this.filterState.size)? Because of this issue, nothing gets run inside of this computed() property. It always return false.
I did a test using setTimeout()
setTimeout(() => {
console.log(this.productGroup.Polarized, this.productGroup.Polarized[this.filterState.size], this.filterState.size);
}, 1000)
//outputs --> "{standard: {mirrored:TRUE: Array(8), mirrored:FALSE: Array(5)}}",
// "{mirrored:TRUE: Array(8), mirrored:FALSE: Array(5)}", <---- correct output
// "standard"
I'm relatively new to Vue.js, I'm not sure if I missed something here with Vue's reactivity. Why does it behave this way? Is there a way to prevent that from happening? I'm happy to provide more info if needed.