What leads to the occurrence of the "maximum call stack size exceeded" error?

I am currently developing a Vue 3 and Bootstrap 5 application. To integrate a date-picker functionality, I opted for the Vue 3 Datepicker plugin available at Vue 3 Datepicker.

Within the file components\Ui\Datepicker.vue, I have included the following code:

<template>
  <datepicker
      @selected="handleSelect"
      v-model="dateSelected" 
      :upper-limit="picked_to"
      :lower-limit="picked_from"
      class="datepicker text-center" />
</template>

<script>
    import { ref } from 'vue';
    export default {
        setup() {
        const dateSelected = ref(new Date());
        return {dateSelected}
        },

        methods: {
           handleSelect() {
             this.$emit('setDate')
           }
        }
    }
</script>

In the file components\Ui\Navigation.vue, the implementation includes:

import Datepicker from './Datepicker' export default { inject: ['$apiBaseUrl'], name: 'Navigation', components: { Datepicker, }, data() { return { // more code } }, methods: { setDate() { this.$emit('setDate'); } }, }

Lastly, in components\Content.vue, the structure consists of:

<template>
  <div class="main">
    <div class="d-sm-flex>
      <h1>{{ title }}</h1>

      <Navigation
        @setDate='setDate'
      />
    </div>

    <div class="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
        tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
        quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
        consequat.</p>
    </div>
  </div>
</template>

<script>
import Navigation from './Ui/Navigation'

export default {
  inject: ['$apiBaseUrl'],
  name: 'Content',
  components: {
    Navigation,
  },

  props: {
    title: String,
  },

  emits: ['setDate'],
  
  data() {
    return {
      headers: {
        'content-type': 'application/json',
        'Accept': 'application/json'
      },

     from: '',
     to: '',

    }
  },

  methods: {
   sendData() {
    this.axios.post(`${this.$apiBaseUrl}/submit`, this.fields, {options: this.headers}).then((response) => {
    if (response.data.code == 200) {
      this.isReport = true;
    }
    }).catch((errors) => {
      this.errors = errors.response.data.errors;
     });
    }
   },
    setDate() {
      console.log('Date');
    },
  }
}
</script>

The issue at hand

Despite selecting a date from the datepicker, the setDate() method fails to execute. Instead, the Chrome console displays the error message:

Maximum call stack size exceeded

Where could the flaw lie?

Answer №1

Previous comments have noted that the error typically arises from an infinite loop scenario. Kissu also highlighted how this can easily occur when there is an event emitted to a parent component, leading to data changes passed as props to a child component, which then triggers another event to the parent.

In the provided code snippet, I couldn't pinpoint any explicit loops, but they may exist in the omitted parts.

Additionally, it's worth mentioning that the infinite loop could be due to the way your component is named. By using "<datepicker ...>" within "Datepicker.vue" without explicitly registering the Datepicker component from "vue3-datepicker," Vue might recursively attempt to mount the component within itself, resulting in a maximum stack trace error.

Your code still has a few issues that need attention.

Firstly:

<template>
  <datepicker
      @selected="handleSelect"
      v-model="dateSelected" 
      :upper-limit="picked_to"
      :lower-limit="picked_from"
      class="datepicker text-center" />
</template>

<script>
    import { ref } from 'vue';
    export default {
        setup() {
        const dateSelected = ref(new Date());
        return {dateSelected}
        },

        methods: {
           handleSelect() {
             this.$emit('setDate')
           }
        }
    }
</script>

You are mixing options and composition API, which is not recommended.

There are also values referenced without clear origins, and you're listening for a non-existent @selected event from the datepicker, as per the documentation.

The same functionality with the composition API would look like this:

<template>
  <Datepicker v-model="initialDate" class="datepicker text-center" />
</template>

<script>
import Datepicker from "vue3-datepicker";
import { ref, watch } from "vue";

export default {
  components: { Datepicker },
  setup(props, { emit }) {
    const initialDate = ref(new Date());

    watch(initialDate, (newDate) => {
      emit("setDate", newDate);
    });

    return { initialDate };
  },
};
</script>

I attempted to replicate your example in a sandbox environment and did not encounter any infinite loop issues.

It would be advisable to review the sandbox link, compare it with your code, address other existing issues first, and observe if that resolves your situation :)

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

Steps for creating an AJAX request to a variable defined within the local scope

I want to create a list using a JSON object that I already have stored in a variable. I have been exploring the dynatable library and its documentation on populating a table using AJAX to receive JSON data. However, I am stuck on how to make it work with ...

Preventing background style from taking precedence over backgroundColor style in React's inline styling

What causes background to take precedence over backgroundColor in React inline-style? In this scenario, the lack of a specified green background-color results in the background gradient with transparency transitioning into white rather than green within t ...

`Dealing with Java Servlet Exception in Kendo UI Environment`

I am facing an issue with displaying date in my Kendo UI grid. The data is coming from a Java servlet, and I have set the status code to 500 whenever an error occurs. Although I can see the error on the console, I am unable to handle it in JavaScript. My g ...

Is there a way to convey a transition MUI property from a parent component to its child component in React?

My current setup involves a parent component with a button that triggers the opening of a snackBar from MUI (child component) upon click. To enhance the user experience, my teacher suggested adding a transition effect to the onClick event so that the snack ...

Encountering a 404 (Not Found) error when attempting to make an API call in React JS with Azure MVC

When trying to make a POST API call from my React app to an Azure MVC controller, I encountered an error in the console: POST http://localhost:3000/api/SampleData/AcknowledgeRole 404 (Not Found) This error is puzzling because I have clearly defined the ...

Opening a Bootstrap Modal in React without relying on npm react-bootstrap

I've been trying to create a Modal in React.js using Bootstrap5, but I'm unable to use npm react-bootstrap for various reasons. I attempted an approach where I utilized state to set Modal classes with a button, which worked well with my NavBar, b ...

Is it advisable to save the text that is utilized in a label?

My journey into web development is just beginning, and I am currently using React JS for my front end development. I have a component that dynamically renders labels based on JSON data, This is how the JSON data looks: data:{ name:"test123" ...

What steps can I take to address the issue of missing modules in an Angular application?

I am facing an issue with my Angular application where I am using two local libraries. Despite having all dependencies declared and imported correctly, the build process continues to throw errors related to missing modules. To give you a better picture of ...

"PHP error: Accessing an undefined offset index 2

I'm currently working on a code snippet where I need to iterate through an array and replace specific characters within the 'color_codes' values. While the code is functioning correctly, I'm encountering an error message stating undefin ...

What could be causing the issue with updating a js file using ajax?

I've been dealing with a php file called users. Initially, everything was going smoothly as I wrote some JavaScript code for it. However, after making updates to the JavaScript code, it seems to have stopped functioning. Below is the content of the p ...

Is there a way to deactivate the onClick event when the dropdown placeholder is chosen?

I have experimented with different methods to prevent the onClick event when selecting either placeholder, but I have not been successful. Here is my current code: <div class="choosesign"> <div class="zodiacs"> < ...

"Encountering a halt in my Node.js Application as it waits endlessly

I'm currently following a tutorial on creating a collaborative canvas drawing application using Node.js and Socket.io. I've included the source file below which is used to create the server. However, when I try to open it in my browser, it gets s ...

The shopping cart in our e-commerce website is refreshed in real-time thanks to the integration of J

I am currently enhancing the Codeigniter Cart with JQuery by making an Ajax call for updates. Below is my JQuery function: $(function() { $('.cart_form select').on('change', function(ev) { var rowid = $(this).attr('c ...

Using dangerouslySetInnerHTML in React within a Fragment

In my current project, I have a specific requirement where I need to format text in React and also include HTML rendering. Here's an example of what I'm trying to accomplish: import React, {Fragment} from "react"; import {renderToString} from " ...

Implementing precise search functionality in a table with jquery datatables

Hey there, I'm attempting to implement an exact search feature in jQuery datatables. In my table, I have a column called "status" with values of either "paid" or "unpaid". Currently, when I type "unpaid", it correctly displays only the unpaid record ...

Ways to resolve the error "Expected an assignment or function call but found an expression with no-unused-expressions"

Issue : Error in ./src/components/main.js Line 7: No function call or assignment found, only an expression is present (no-unused-expressions) Search for the specific keywords to get more information on each error. import React from 'react'; ...

Ways to update row background color based on specific column values

I need to customize the background color of my table rows based on the value in the "Category" column. For example: Name Category Subcategory A Paid B C Received D If the Category value is 'Paid', I want the ro ...

Leveraging the power of Bootstrap and JavaScript to implement a custom Toast feature

I am utilizing Bootstrap 4 to design Toasts, and I am presently developing a JavaScript function to generate a toast. My attempt to create elements within the JS file did not properly style it. index.html <!doctype html> <html lang="en"> ...

Tips for managing CSS/style conflicts in JavaScript/AngularJS applications

I am new to AngularJS and I have a requirement to assign CSS properties to a component at the JavaScript file level. When I debug my code after applying the CSS styles to the component, I can see that all the applied CSS properties are behaving as expected ...

Dynamic Bootstrap Modal Widget

I have been attempting to create a dynamic modal using Twitter Bootstrap as shown below: $('#myModal').on('hide', function () { $(this).removeData('modal'); $('.loading-modal').remove(); }) Although it rel ...