Where is the loop infinity?












1















I find myself with an infinite loop error but I don’t see where it is. That’s when I add class.



template :



  <tr class="listOfDay">
<td>Jour de la semaine</td>
<td v-for="day in nbDaysInMonth" :key="index" :class="{weekend: isWeekend}">{{dayOfWeek(day,index)}}</td>
</tr>


script :



data: () {
isWeekend: false,
},
methods : {
dayOfWeek(day) {
var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day();
if(d === 5 || d === 6) {
this.isWeekend = true
} else {
this.isWeekend = false
}
return this.days[d]
}
}









share|improve this question


















  • 2





    Could you please provide a reproduction on codesandbox.io ?

    – Dimitri Kopriwa
    Jan 3 at 16:53






  • 2





    First of all, this.nbDaysInMonth, this.currentYear, this.nbMonth and this.days seems undefined in your script example.

    – Thomas Brd
    Jan 3 at 16:56













  • Michael is right, it's re-rendering because you're using changing the value isWeekend during render, which is causing the infinite loop. If you'd like more input on how to solve this issue though, you'll need to provide more code.

    – Daniel
    Jan 3 at 17:52
















1















I find myself with an infinite loop error but I don’t see where it is. That’s when I add class.



template :



  <tr class="listOfDay">
<td>Jour de la semaine</td>
<td v-for="day in nbDaysInMonth" :key="index" :class="{weekend: isWeekend}">{{dayOfWeek(day,index)}}</td>
</tr>


script :



data: () {
isWeekend: false,
},
methods : {
dayOfWeek(day) {
var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day();
if(d === 5 || d === 6) {
this.isWeekend = true
} else {
this.isWeekend = false
}
return this.days[d]
}
}









share|improve this question


















  • 2





    Could you please provide a reproduction on codesandbox.io ?

    – Dimitri Kopriwa
    Jan 3 at 16:53






  • 2





    First of all, this.nbDaysInMonth, this.currentYear, this.nbMonth and this.days seems undefined in your script example.

    – Thomas Brd
    Jan 3 at 16:56













  • Michael is right, it's re-rendering because you're using changing the value isWeekend during render, which is causing the infinite loop. If you'd like more input on how to solve this issue though, you'll need to provide more code.

    – Daniel
    Jan 3 at 17:52














1












1








1








I find myself with an infinite loop error but I don’t see where it is. That’s when I add class.



template :



  <tr class="listOfDay">
<td>Jour de la semaine</td>
<td v-for="day in nbDaysInMonth" :key="index" :class="{weekend: isWeekend}">{{dayOfWeek(day,index)}}</td>
</tr>


script :



data: () {
isWeekend: false,
},
methods : {
dayOfWeek(day) {
var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day();
if(d === 5 || d === 6) {
this.isWeekend = true
} else {
this.isWeekend = false
}
return this.days[d]
}
}









share|improve this question














I find myself with an infinite loop error but I don’t see where it is. That’s when I add class.



template :



  <tr class="listOfDay">
<td>Jour de la semaine</td>
<td v-for="day in nbDaysInMonth" :key="index" :class="{weekend: isWeekend}">{{dayOfWeek(day,index)}}</td>
</tr>


script :



data: () {
isWeekend: false,
},
methods : {
dayOfWeek(day) {
var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day();
if(d === 5 || d === 6) {
this.isWeekend = true
} else {
this.isWeekend = false
}
return this.days[d]
}
}






javascript vue.js vuejs2 momentjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 16:49









DenisMasotDenisMasot

1181414




1181414








  • 2





    Could you please provide a reproduction on codesandbox.io ?

    – Dimitri Kopriwa
    Jan 3 at 16:53






  • 2





    First of all, this.nbDaysInMonth, this.currentYear, this.nbMonth and this.days seems undefined in your script example.

    – Thomas Brd
    Jan 3 at 16:56













  • Michael is right, it's re-rendering because you're using changing the value isWeekend during render, which is causing the infinite loop. If you'd like more input on how to solve this issue though, you'll need to provide more code.

    – Daniel
    Jan 3 at 17:52














  • 2





    Could you please provide a reproduction on codesandbox.io ?

    – Dimitri Kopriwa
    Jan 3 at 16:53






  • 2





    First of all, this.nbDaysInMonth, this.currentYear, this.nbMonth and this.days seems undefined in your script example.

    – Thomas Brd
    Jan 3 at 16:56













  • Michael is right, it's re-rendering because you're using changing the value isWeekend during render, which is causing the infinite loop. If you'd like more input on how to solve this issue though, you'll need to provide more code.

    – Daniel
    Jan 3 at 17:52








2




2





Could you please provide a reproduction on codesandbox.io ?

– Dimitri Kopriwa
Jan 3 at 16:53





Could you please provide a reproduction on codesandbox.io ?

– Dimitri Kopriwa
Jan 3 at 16:53




2




2





First of all, this.nbDaysInMonth, this.currentYear, this.nbMonth and this.days seems undefined in your script example.

– Thomas Brd
Jan 3 at 16:56







First of all, this.nbDaysInMonth, this.currentYear, this.nbMonth and this.days seems undefined in your script example.

– Thomas Brd
Jan 3 at 16:56















Michael is right, it's re-rendering because you're using changing the value isWeekend during render, which is causing the infinite loop. If you'd like more input on how to solve this issue though, you'll need to provide more code.

– Daniel
Jan 3 at 17:52





Michael is right, it's re-rendering because you're using changing the value isWeekend during render, which is causing the infinite loop. If you'd like more input on how to solve this issue though, you'll need to provide more code.

– Daniel
Jan 3 at 17:52












1 Answer
1






active

oldest

votes


















4














You're changing isWeekend as the component is rendered, therefore causing the component to rerender, I believe this is causing the infinite loop.



Don't calculate isWeekend as part of dayOfWeek. I think the best solution would be to create another method.



EDIT: as suggested in a comment, using computed values is probably an even better solution.






share|improve this answer





















  • 1





    also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

    – Daniel
    Jan 3 at 17:20






  • 1





    Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

    – Michael Pratt
    Jan 3 at 17:40











  • Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

    – DenisMasot
    Jan 4 at 10:09














Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026500%2fwhere-is-the-loop-infinity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









4














You're changing isWeekend as the component is rendered, therefore causing the component to rerender, I believe this is causing the infinite loop.



Don't calculate isWeekend as part of dayOfWeek. I think the best solution would be to create another method.



EDIT: as suggested in a comment, using computed values is probably an even better solution.






share|improve this answer





















  • 1





    also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

    – Daniel
    Jan 3 at 17:20






  • 1





    Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

    – Michael Pratt
    Jan 3 at 17:40











  • Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

    – DenisMasot
    Jan 4 at 10:09


















4














You're changing isWeekend as the component is rendered, therefore causing the component to rerender, I believe this is causing the infinite loop.



Don't calculate isWeekend as part of dayOfWeek. I think the best solution would be to create another method.



EDIT: as suggested in a comment, using computed values is probably an even better solution.






share|improve this answer





















  • 1





    also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

    – Daniel
    Jan 3 at 17:20






  • 1





    Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

    – Michael Pratt
    Jan 3 at 17:40











  • Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

    – DenisMasot
    Jan 4 at 10:09
















4












4








4







You're changing isWeekend as the component is rendered, therefore causing the component to rerender, I believe this is causing the infinite loop.



Don't calculate isWeekend as part of dayOfWeek. I think the best solution would be to create another method.



EDIT: as suggested in a comment, using computed values is probably an even better solution.






share|improve this answer















You're changing isWeekend as the component is rendered, therefore causing the component to rerender, I believe this is causing the infinite loop.



Don't calculate isWeekend as part of dayOfWeek. I think the best solution would be to create another method.



EDIT: as suggested in a comment, using computed values is probably an even better solution.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 3 at 17:41

























answered Jan 3 at 16:59









Michael PrattMichael Pratt

2,4771126




2,4771126








  • 1





    also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

    – Daniel
    Jan 3 at 17:20






  • 1





    Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

    – Michael Pratt
    Jan 3 at 17:40











  • Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

    – DenisMasot
    Jan 4 at 10:09
















  • 1





    also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

    – Daniel
    Jan 3 at 17:20






  • 1





    Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

    – Michael Pratt
    Jan 3 at 17:40











  • Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

    – DenisMasot
    Jan 4 at 10:09










1




1





also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

– Daniel
Jan 3 at 17:20





also avoid using methods where you can use computed values. Reduces issues with mutablility and improves rendering efficiency.

– Daniel
Jan 3 at 17:20




1




1





Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

– Michael Pratt
Jan 3 at 17:40





Yeah, probably even better, but I'm not overly familiar with Vue. I've seen the same (anti)pattern in React though so recognized it.

– Michael Pratt
Jan 3 at 17:40













Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

– DenisMasot
Jan 4 at 10:09







Thanks for your help. I create two methods. Template : :class="{weekend: isWeekend(day)}">{{dayOfWeek(day)}} Script : dayOfWeek(day) { var d = moment(new Date(this.currentYear + '-' + this.nbMonth + '-' + day)).day(); return this.days[d] }, isWeekend(day) { return this.dayOfWeek(day) === "S" || this.dayOfWeek(day) === "D" }

– DenisMasot
Jan 4 at 10:09






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026500%2fwhere-is-the-loop-infinity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Can't read property showImagePicker of undefined in react native iOS

Pushsharp Apns notification error: 'InvalidToken'