Why does updating state lead to a re-rendering loop while setting state does not encounter the same issue?

Essentially, I have developed a REST Api that retrieves a list of components (sections) from a CMS along with their associated data. Each section in the UI is assigned a number to indicate its position, but certain sections are excluded from counting, such as a career block section. This numbering information is passed as a prop to each rendered section using the format :number=""

In my function, it iterates through the sections and increments the number if the component is included in the allowedNumerableSections array. If the component is not in the array, it will return false and increment the decrementAmount state variable.

For example, let's assume we have 5 sections:


Section1
Section2
Section3
Section4
Section5

If Section3 is not included in the allowedNumerableSections array, then when these sections are looped out with the :number prop for each section, it would look like:


<Section1 number="1" />
<Section2 number="2" />
<Section3 number="false" />
<Section4 number="3" />
<Section5 number="4" />

Therefore, Section3 is skipped in the numbering sequence. Consequently, the UI might display /01 for Section 1 and /03 for Section 4, indicating the correct numbering order.

The issue arises when I implement logic to increment the state variable. For some reason, it loops specifically 608 times and the final numbers end up showing something like /0-201 instead of /04, or /0-204 instead of /01.

Here is an excerpt of my code:


<template>
  <div class="sections">
    <component
      :is="getSection(section.__component)"
      v-for="(section, index) in sections"
      :key="index"
      :section="section"
      :number="getNumber(section, index)"
      class="section"
    />
  </div>
</template>

<script>
export default {
  name: "SectionStructure",

  ... // Components, props, data, methods, computed properties

};
</script>

However, when I manually set decrementAmount to a specific value like this.decrementAmount = 3, it correctly loops the desired number of times, although all indexes are reduced by 3. This outcome is not ideal.

Does anyone have insights into why this continuous looping behavior occurs?

Answer №1

It seems that with each re-render, the function getNumber recalculates, subsequently updating the value of number, creating a continuous loop. Have you considered assigning the value of number upon receiving the response, but before the initial render? (e.g. on mount, or preferably in the response handler)

mounted() {
  this.calculateNumber()
},
methods: {
  calculateNumber(apiResult) {
    let decrementAmount = 0
    apiResult.map(section => ({
      ...section,
      number: this.getNumber()
    }))
  }
}

Regarding the issue of getting 608, it is likely due to reaching the recursion limit. Unfortunately, I cannot provide specifics on how this limit is determined.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

CORS issue encountered by specific user visiting the hosted website

I recently developed a bot chatting website using Django and React, which I have hosted on HOSTINGER. The backend is being hosted using VPS. However, some users are able to see the full website while others encounter CORS errors where the APIs are not func ...

The NestJS HttpException class will default to a status code of 201 if no specific status code is

This particular instance showcases how instantiating the HttpException class in this manner results in an exception being thrown with an undefined status, ultimately becoming a status of 201 (I presume it defaults to this status as it is associated with a ...

Converting a string to regular text in JavaScript (specifically in ReactJS)

When I fetch data from an API, sometimes there are special characters involved. For example, the object returned may look like this: { question : "In which year did the British television series &quot;The Bill&quot; end?" } If I save t ...

Swapping values in JSON by comparing specific keys: A guide

I have JSON data that contains a key called reportData with an array of values: {"reportData":[ ["1185","R","4t","G","06","L","GT","04309","2546","2015","CF FE","01H1","20","23840","FF20"], ["1186","R","5t","R","01","L","TP","00110","1854","2016" ...

Elegant switch in jQuery

I am trying to use jQuery to create an animated toggle button. The toggle function is working correctly, but I am having trouble adjusting the speed and smoothness of the animation. After researching different methods and attempting to modify the values i ...

How can I track and log the name of the country each time it is selected by a click?

I'm currently utilizing VueJS along with a REST API and axios to retrieve the list of countries, then showcasing them in card format on the webpage. However, I am facing a challenge in creating a history list that captures the last 5 countries clicked ...

Tips for adding a bounding box to an image received from the server

I've got a python server that is returning a collection of bounding boxes post OCR processing (using Tesseract or similar). "bbox": [{ "x1": 223, "y1": 426, "x2": 1550, &q ...

Obtain Rails database queries in real-time

Is it possible to retrieve database results dynamically in Rails? For instance, if I have a list of cities indexed by ID, can I pass the ID to the view based on the city name, similar to this JavaScript method (even though I know this code won't work) ...

Issue with Angular Route Guard - Incorrect redirection to login page

I'm encountering an issue with my Angular app where even after the JWT token has expired, I am still able to navigate within the application without any API data being accessible. I've double-checked my setup and it seems right, but for some reas ...

Retrieve weight measurements from a serial port by using Node JS

I'm a novice in node.js and I'm trying to retrieve weight data from a serial port. I have nodejs(v14.19.0) and npm(6.14.16) installed, and I'm attempting to obtain weight data using localhost:8080/get_weight. However, the script isn't f ...

What is the process for creating unit tests for a method that utilizes the @Transaction() decorator?

I am currently using NestJS 6 and TypeORM to interact with a MySQL database. While attempting to write unit tests for a method that utilizes the @Transaction() and @TransactionManager() decorators, I encountered the following error message: ConnectionNotF ...

Difficulty arising from implementing v-if as a filter within a v-for loop in Vue.js

I'm struggling a bit with setting up a conditional statement using v-if along with a loop using v-for in Vue. Here's what I have so far: <div class="row form-group" v-for="(article, key, index) in articles" :key="key" v-if="article.pubdate(fi ...

The jQuery button function is not compatible with Google Graph

Upon review below, I have successfully created the getChart() function. It functions properly when called independently, however, it fails to display the chart when enclosed within $(document).ready(function(){...}). The file is also attached for referen ...

Show concealed elements above everything else

I've encountered an issue with my custom dropdown list as it displaces other elements when it appears. I want it to overlay on top of everything else, similar to the default select behavior. Despite trying to set position: relative; and z-index:100;, ...

jquery plugin for creating Excel-like filters

I am in the process of creating Excel-like filtering for my dynamic table. To achieve this, I am utilizing a jQuery plugin that can be found at https://github.com/chestercharles/excel-bootstrap-table-filter. The reason why I chose this plugin is because it ...

The jquery datepicker is malfunctioning after switching to another component

My current setup includes the following versions: jQuery: 3.3.1 jQuery UI: 1.12.1 AngularJS: 6 Here's a snippet of my code: <input id="test" type="text" class="form-control" value=""> In my component (component.t ...

Determine whether the user has scrolled past a specific threshold

Is there a way to display the button with the id backtotop only when the user has scrolled more than 250 pixels? This is my code: <template> <div> <button v-if="checkScroll" class="goTop" @click="backToT ...

I am experiencing some difficulty with the GetServerDate using the JSON protocol in JQuery; it's

I am facing an issue while trying to execute a basic ajax call to fetch data from a file on a server. Even though I can access the file via the web browser and have diligently followed tutorials related to this topic, I have hit a dead end. Here is the sn ...

Exploring Ways to Navigate to a Component Two Steps Back in Angular

Let's say I have three routes A->B->C. I travel from A to B and then from B to C. Now, is it possible for me to go directly from C to A? ...

Activating JavaScript in the browser only when a button is manually clicked, not by default

As I work on my website, which is currently being constructed, I rely heavily on JavaScript. However, a concern of mine is the potential for failure if a user has JavaScript disabled on their client side system. I understand that it is not possible to pro ...