When using Vue computed, an error is thrown stating "Issue in rendering: 'InternalError: too much recursion'" if the same key is referenced in computed

As a newcomer to Vue, I wanted to track how many times a function in the computed section gets called. To achieve this, I created the following component:

const ComputedCounter = {
    name: "ComputedCounter",
    template: `
        <span>{{ value }}</span>
    `,
    computed: {
        value() {
            const current = this.value || 0;
            return current + 1;
        }
    }
}

Unfortunately, I encountered an error saying 'Error in render: "InternalError: too much recursion"'. I'm not sure why this is happening and if there's a solution to make it work properly.

Answer №1

When delving into the documentation, it states:

A computed property is triggered to re-evaluate only when its reactive dependencies have been altered.

This implies that if the value this.value changes, then the associated computed property will be activated. However, the issue arises when executing the computed property results in a change to value. Consequently, since value has changed, the computed property is executed once again, causing another change to value, initiating a never-ending cycle of execution. I hope this clarifies the situation.

Answer №2

There seems to be a missing stop condition in your recursive function.
Exceeding the maximum call stack size is caused by excessive recursion, indicating a JavaScript problem rather than a Vue issue.

Answer №3

To begin, update your data with the following:

_data: 0

Afterwards, you will be able to access this._data within your getData() function:

getData() {
    return this._data++;
}

Answer №4

If you want to keep track of how many times a computed function is called, it's important to avoid giving the counter and the computed function the same name. This can lead to the computed function calling itself infinitely, creating an endless loop.

A better approach would be to use separate variables for the counter and the computed function. Here's an example:

const ComputedCounter = {
    name: "ComputedCounter",
    template: `
        <span>{{ computedValue }}, computedValue has been called {{ counter }} times</span>
    `,
    data() {
      return {
        counter: 0,
        value: 0,
      };
    },
    computed: {
        computedValue() {
           this.counter++; // Avoid side effects in computed functions
           return this.value;
        }
    }
}

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

The error callback for Ajax is triggered even though the JSON response is valid

Within my JavaScript file, I am making the following call: $.ajax({ type: "POST", dataType: "application/json", url: "php/parseFunctions.php", data: {data:queryObj}, success: function(response) { ...

Tips for inserting a hyperlink on every row in Vuetables using VUE.JS

Trying to implement a link structure within each row in VUEJS Vuetables has been challenging for me. Despite my research on components and slots, I am struggling to add a link with the desired structure as shown below: <td class="text-center"> <a ...

Here is a way to retrieve the name of a ref object stored in an array using Vue.js 3 and Typescript

I have a Form, with various fields that I want to get the value of using v-model and assign them to ref objects. In order to populate my FormData object with this data, I require both the name and the value of the ref objects. Unfortunately, I am struggli ...

Ways to adjust your selection to the space or new line before or after

$('button').on('click', function(){ const selection = window.getSelection(); selection?.modify('move', 'backward', 'word'); selection?.modify('extend', 'forward', 'to the next space ...

Is it possible to employ a filter within a v-for loop?

When using a v-for loop, I am trying to apply a filter to the text. Here is my inline template: <select> <option v-for="(item, index) in items" :key="index" :value="item.id" v-text="item.name | capitalize&qu ...

How does assigning a checkbox's value as 'undefined' result in the addition of ng-invalid?

I'm facing an issue with a checkbox in my Angular project. The checkbox has a false value of undefined, and even though it's not marked as required, the form doesn't validate when I check and uncheck it. This is because the class ng-invalid ...

Retrieve the IDs of list elements in order to dynamically update a div using AJAX

I'm facing a bit of a challenge at the moment. I have a webpage that uses jQuery to load three different views: one displaying all categories associated with the user, another showing the image and name of items within the selected category, and a thi ...

prevent parent jQuery functions from interfering with child function execution

I am facing an issue with conflicting jQuery functions between parent and child elements. The child function is a bootstrap function, while the parent is a custom function. The main objective is to limit the height of the parent div (refer to the code belo ...

Ensure you are focusing on elements exclusively contained within the parent table and not any child tables nested within its cells

Looking for some help with a unique situation here. I'm struggling to find the right selector for this task. For reference, you can check out the jsfiddle at this link: http://jsfiddle.net/NZf6r/1/ The scenario is that I have a parent table containin ...

Trying to use 'cpa' before it is initialized will result in an error

I encountered the error stated above while using this particular route: router.put('/edit/:id', async (req, res) => { const cpa = await CPASchema.findById(req.params.id).then(() => { res.render('edit', { cpa:cpa }); cons ...

Tips for displaying a slideshow within a div using JavaScript

When the HTML loads for the first time, the div slideshow remains hidden until a button is clicked. Any suggestions on how to display the first div without requiring a button click? var slideIndex = 1; showDivs(slideIndex); function plusDivs(n) { s ...

Having trouble with Google Maps Places API autocomplete feature; it's not functioning properly

My goal is to integrate the Google Maps Places API with a search box. To test jQuery autocomplete, I managed to make it work using this simple example: function bindAutocomplete() { var locationSearch = $("input#Location"); locationSearch.autocom ...

Setting up an event listener for a newly added list through the use of node appendChild

I am currently working on dynamically adding a select list to my HTML document. While I have successfully added the node to the DOM, I am struggling with creating an event listener in a separate JavaScript file that recognizes the newly created select list ...

Storing a function value in a variable within a Node.js server to retrieve folder size

I have been searching for a way to determine the size of a folder's contents and discovered that using the get-folder-size package is the best option. The code snippet below explains how to achieve this: const express = require('express'); ...

Unlocking fashion with $refs: A step-by-step guide

After using console.log(this.$refs['block' + row + column]);, I confirmed that I can successfully access the HTML element it refers to. https://i.stack.imgur.com/7KYqB.png However, when attempting to access the element's style using variou ...

Implementing multiple filters with jQuery

Make a Selection `<select class="form-control" id="technology"> <option name="sort" value="2g" id="2g"gt;2G</option> <option name="sort" value="3g" id="3g"&g ...

The sorting function in Vue.js, _.orderBy, seems to be having trouble sorting

UPDATE This is the json data retrieved through an API using axios. bannerData= [ { "id": 118, "title": "Geruchsbel\u00e4stigung", "location": "DOR", "pressInformation": [ { ...

Get the ID from a row that was created dynamically

I have a webpage that pulls information from my database and displays it. Users should be able to click on the "details" link to see more details about a particular record, or click on an input box in the "check" column and submit to update the record stat ...

Are there any comparable features in Angular 8 to Angular 1's $filter('orderBy') function?

Just starting out with Angular and curious about the alternative for $filter('orderBy') that is used in an AngularJS controller. AngularJS example: $scope.itemsSorted = $filter('orderBy')($scope.newFilteredData, 'page_index&apos ...

Guide on how to retrieve a specific object element within a Vue.js list using Nuxt

I've got an array of objects structured like this: { id: 33617, datePublication: 1532266465, dateUpdate: 1532266574, headline: 'A catchy headline goes here', images: [ [Object] ] }, { id: 33614, datePublication: 1532265771, date ...