0

I've got this "basic" function which is checking if [i] element in array is the same as id:

    checkArray(offer, id){
        if (id)
        {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id)
                {
                  console.log("bam!")
                  // this.count=+1;
                  return true;
                  break;
                } 
            }
            return false;
        }
        else {
            return true;
        }
    },

variable count is declared in vuejs data

data() {
  return {
    count: 0 
  } 
}

checkArray is called from v-show:

<v-layout  row wrap v-for="offer in offers.slice((current_page-1)*5, current_page*5)" 
v-show="checkArray(offer, speciality)">

at this point all works well. I've got two bams.

Now when I uncomment this.count=+1; I've got 200 bams! and my vuejs console screams:

[Vue warn]: You may have an infinite update loop in a component render function.

Why is this happening? How can I count number of bams in variable?

18
  • 3
    It isn't an infinite loop, it's a syntax error. You can't have for (i=0 in offer.specialities). You can have for (i in offer.specialities), but not with =0. Commented Jan 30, 2018 at 18:26
  • 1
    i=0 in offer.specialities what should that do?? Commented Jan 30, 2018 at 18:27
  • 1
    @JonasW.: If it were, it would need two ;s in it. Commented Jan 30, 2018 at 18:28
  • Maybe the infinite loop is where checkArray is called Commented Jan 30, 2018 at 18:30
  • 1
    @gileneusz Where are you calling checkArray? ... can you provide the template piece of your code? What is most probably happening is that using this.count in your checkArray method is mutating the state over and over again, causing the component to re-render every time. Commented Jan 30, 2018 at 18:46

1 Answer 1

3

Vue thinks you have an infinity loop because you read and modify the count variable inside the same loop.

Because you read the variable count in the loop, vue will start to watch the count variable for any updates.

Because you write the count variable, vue will rerun every listeners the next tick.

You should delegate the computation of your loop body into a seperate computed properties.

currentPageView() {
    return this.offers.slice((current_page-1)*5, current_page*5);
},

shownPageView() {
    const result = [];
    for(let i = 0; i < currentPageView.length; i++) {
        const offer = currentPageView[i];
        const id = this.speciality;
        if (id) {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id) {
                  result.push(offer);
                  break;
                } 
            }
        } else {
             result.push(offer);
        }
    }
    return result;
},

countSpecialOffers() {
    let count = 0;
    for(let i = 0; i < currentPageView.length; i++) {
        const offer = currentPageView[i];
        const id = this.speciality;
        if (id) {
            this.count=0;
            for (var i in offer.specialities) {
                if (offer.specialities[i] == id) {
                  count++;
                  break;
                } 
            }
        }
    }
    return count;
}

After doing this, you can access both shownPageView to loop over your results, and countSpecialOffers to get the count of the special offers

Sign up to request clarification or add additional context in comments.

1 Comment

this is the answer, I had no idea why this happened

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.