Double firing of beforeUpdate event observed in Vue for recursive components

Recently delving into the world of vue js, I've been tinkering around with a recursive tree example inspired by the official example found here.

While the original example starts with treedata containing a single root and multiple children, my goal was to expand this to include multiple root elements, as showcased in this example.

In my own example, I observed that the beforeUpdate method is triggered twice for every component click. Even when attempting to directly add elements into children, the beforeUpdate is still called twice. Conversely, in the official recursive example, this only happens once (verified using vue-devtools). I'm puzzled by this behavior and would appreciate any insights on why it occurs.

Could this be linked to using v-for in li instead of the actual component name?

UPDATE: I included a datetime to track which elements are being re-rendered, and it appears that in my example, all children are re-rendered, as evidenced by the time displayed on the component.

Answer №1

One of the reasons why your onUpdate function is being triggered twice is because it gets called for both the parent component and its nested child component. To verify this, I included a unique identifier for each component instance and displayed it in an alert.

The project you mentioned only modifies the state within the clicked component node due to the fact that the entire tree is already rendered using the v-show directive instead of an v-if directive.

In your specific example, new nodes are being added to the data, resulting in the child component being rendered and the parent component being updated as well.

Answer №2

I want to express my gratitude to Steven Spungin for assisting me in identifying the components' IDs that aided in debugging this issue.

Upon close examination, I have identified the root cause of the problem. It appears that the issue lies in the following code snippet:

<li v-for="(filter, index) in filters"
     v-bind:key="index"
     v-on:click.self="addfilter(filter)">

    {{filter.name + new Date()}}

    <template v-if="filter.children">
        <filter-tree
         v-bind:filters="filter.children">
        </filter-tree>
    </template></div>

</li>

It seems that the loop on the `li` element is creating a single parent with multiple child `filter-tree` components, which is not the intended behavior. It appears that a recursive call should handle the creation/rendering of itself and child components using the `v-for` on `filter-tree`, as demonstrated in the official example.

For a solution, please refer to the revised version of my example: https://jsfiddle.net/z3sekaqy/

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

What are some ways to reuse an angular component multiple times?

I am utilizing an angular component to dynamically load charts into my widget: Below is an example of the component: angular.module("generalApp").component("chartPie", { template: "<div class=Pie></div>", bindings: { data: "=", }, control ...

Having trouble with Laravel 5.5 and ajax file uploads?

I am encountering an issue with Laravel 5.5 while trying to get an ajax file. I can successfully receive the file using $_FILES, but $request->file() is not working as expected. Here is the code in question. Below are the HTML & Ajax elements: <htm ...

Find a specific number within an array using Javascript, and if it is not found, return the closest

In Javascript, I want to find a number in an array. If the number does not exist in the array, I need to return a number that is lower than the target number. ...

Guide to integrating Firebase token authentication with a web API2 controller

In order to pass a Firebase authentication token to a web API controller, I am following steps outlined in this StackOverflow post: stackoverflowpost The bearer token is included in the $http request headers. https://i.sstatic.net/GTJz0.png Despite addr ...

Utilizing JSON information acquired through AJAX within a distinct function

Sorry if this question has been asked before, I tried following suggestions from another post but they didn't work for me. What I'm trying to do is fetch some JSON data, save a part of it in a variable, and then use that variable in a different f ...

Exploring the possibilities of utilizing dynamic components in Nuxt.js 3

I am attempting to display dynamic components but it is not functioning as expected. <component :is="comp" /> The variable comp contains the name of the component during the process (CardComponent). No error messages are being shown and n ...

Issues arise when attempting to use two HTTP get requests in a single AngularJS controller

Having two http GET requests in one controller, causing intermittent issues where only one of the GET requests works or sometimes none of them are shown. Any suggestions? }).controller("nextSidorAdminCtrl", function($scope,$rootScope,$http,$location,$s ...

Why is it necessary to use 'this.' to reference a function inside a Component in React?

class First extends React.Component{ handleClick(){ alert('handle click'); } render(){ return <div> <button onClick={this.handleClick}>click</button> </div>; } } Explo ...

Upgrade the WordPress light editor to the advanced version

After developing a script to upgrade the WordPress editor on a specific page from light mode to Advanced once a user clicks the Unlock button and confirms their desire to make the switch, an issue arose. Despite deducting 5 coins from the user's balan ...

Instancing prohibits me from adjusting the transparency or opacity of each specific child geometry

I am currently working on a project that involves a simple model made up of 1000 instances of spheres. In an effort to optimize performance by reducing the number of draw calls, I decided to implement instancing. However, I encountered an issue with changi ...

Manipulating JSON arrays in Javascript - Remove an object from the array

I'm looking to remove an element from a JSON objects array. Here's the array: var standardRatingArray = [ { "Q": "Meal", "type": "stars" }, { "Q": "Drinks", "type": "stars" }, { "Q": "Cleanliness", "type": " ...

The perplexing actions of Map<string, string[]> = new Map() have left many scratching their heads

I encountered an issue while trying to add a value to a map in my Angular project. The map is initially set up using the following code: filters: Map<string, string[]> = new Map(); However, when I attempt to add a value to this map, it starts displa ...

The visual representation of my data in Highchart appears sporadic, with the points scattered rather than following a clean, linear progression from left to right

I am working on displaying a 2D array [timestamp, count] on a highchart for the past 90 days from left to right. However, I am facing an issue where the chart appears sporadic when introduced to production data. It works fine with smaller datasets. After ...

Modify the value of a service from the main controller

I have been researching extensively on how to edit a service value from a nested controller. The issue I am facing is that my child controller needs to update a specific value in a service, and this value must be reflected in the parent controller as well. ...

Tips for using an array as a jQuery selector in JavaScript

Managing an element with an array id id="x[]" can be tricky when it comes to dynamically changing content based on database entries. This specific element is essentially a delete button for removing table rows from the database. <div align="center" id= ...

Learning the integration of vuex with defineCustomElement in Vue 3.2

V3.2 of Vue introduces the ability to create custom elements with the defineCustomElement feature. Learn more here. Can anyone explain how to connect a store (Vuex) with a defineCustomElement? ...

How can I transform an array of text into dropdown options?

Currently, while utilizing Vue and vue-bootstrap, I am facing the task of populating a dropdown menu with an array of text items retrieved from my database. The <b-form-select> component in Bootstrap requires objects of the form {value: 'some va ...

Implementing CSS styles to Iframe content

I have written the following code snippet to display an iframe: <iframe scrolling='no' frameborder='1' id='fblike' src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.XYZ.com%2F&amp;send=false& ...

Following the upgrade to version 6.3.3, an error appeared in the pipe() function stating TS2557: Expected 0 or more arguments, but received 1 or more

I need some assistance with rxjs 6.3.3 as I am encountering TS2557: Expected at least 0 arguments, but got 1 or more. let currentPath; const pipeArgs = path .map((subPath: string, index: number) => [ flatMap((href: string) => { con ...

Implement a Bootstrap button that can efficiently collapse all elements in one click

Within my HTML file, I have included the following code: <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <div class="list-group list-group-flush"> <a href="javascript: void(0)" da ...