Vue.js - Resetting child components upon array re-indexing

I am working with an array of objects

const array = [
  {
    id: uniqueId,
    childs: [
      {
        id: uniqueId
      }
    ]
  },
  {
    id: uniqueId,
    childs: [
      {
        id: uniqueId
      }
    ]
  },
]

and I have a looping structure in place

<template v-for="(item, index_item) in array" :key="item.id">
  <div>
    <template v-for="(child, index_child) in item.childs" :key="child.id">
      <button type="button" @click.prevent="addChild(index_item, index_child)"></button>

      <FileUploader></FileUploader>

      <button type="button" @click.prevent="addChild(index_item, index_child + 1)"></button>
    </template>
  </div>
</template>

and I am using the addChild method as follows

const addChild = (index_item, index_child) => {
  array[index_item].childs.splice(index_child, 0, {id : uniqueId}
}

When adding a child to the end of the array, everything works fine. However, if the indexes change, the file inputs inside FileUploader components get reset. I've tried different methods like setting a unique key for FileUploader, but no luck.

I need assistance on how to re-index the array without resetting my components

Any advice would be greatly appreciated.

Answer №1

Amazing Vue Playground

Your code might have repeated uniqueId for array elements. If so, rectify by assigning genuine unique IDs to each array element.

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

Removing an element in ReactJS

As a newcomer to React, I created an application that supports saving searches. Currently, it retrieves data from a static array data in JSON format. Unfortunately, I'm struggling with the process of removing saved searches from the list. You can vie ...

Unable to carry out specific tasks in a particular order with GULP

I'm having trouble getting the TASK cssmin to execute after scss. It works fine when the folder destination is not empty and .css files are already compiled with the 'scss' task, but if the folder is empty, the cssmin task doesn't creat ...

Extract data from JSON fields and assign them to variables

I am attempting to extract specific fields from a JSON response received from an API query. Here is a sample JSON: { "_id":"611a7b651571d300074b9875", "Details":{ "Source":{ "Type":&qu ...

The submit button fails to produce any result

I have a submit button in a contact form that triggers PHP code to send an email. However, when I press the button nothing happens - not even the PHP code is displayed as it should be if PHP were not installed. Interestingly, I have tested other simple mai ...

I can't find my unit test in the Test Explorer

I'm currently working on configuring a unit test in Typescript using tsUnit. To ensure that everything is set up correctly, I've created a simple test. However, whenever I try to run all tests in Test Explorer, no results are displayed! It appear ...

Modify the array output so that it no longer contains brackets

After transforming a sparse dictionary into an array using (np.asarray), I created a function that utilizes this array to calculate a formula. However, the output generated includes double brackets instead of the desired single bracket format. For example, ...

unable to use 'await' keyword to delay the code execution until a function finishes

I'm encountering an issue where I need to wait for the result of a local function before proceeding further. Here is the code for the local function: var Messagehome = (req, res) => { user.find().exec(async (err, user) => { if (err) ret ...

Encountering difficulties with "removeChild" in React when attempting to dynamically update an array

In my React component's state, I have an array called "personArray." The component includes a search bar that triggers an API request and updates the "personArray" based on user input. In the render method, I map through this "personArray" to display ...

What is the best method for excluding past dates in the Ui calendar?

As a beginner with Ui calendar, I am seeking guidance on how to prevent users from selecting or interacting with previous dates in Ui-calendar using angularjs. While the Eventdrop, EventResize, and eventclick features are functioning properly for me, it ...

Leveraging and utilizing TypeScript npm packages

My goal is to create shared code in TypeScript, package it as an npm package, and easily install it. I attempted to create an external library like this: export class Lib { constructor(){ } getData(){ console.log('getting data from l ...

Discover the total value of the elements in a json array within mysql while applying filtering

I'm currently using mariadb version 10.2.43-MariaDB-1:10.2.43+maria~bionic Here is the schema of the table: The table consists of a column called id and an array named details, which contains the following two sets of data: { "id": 9, ...

Searching for specific data within a dataset using data row filtering in C# ASP.NET

I am working on a page that displays data from two tables populated by a database - one with existing operators and another with existing users. If a user is also listed as an operator, I want to hide their row in the table. Below is the code I have writte ...

"Exploring the best way to loop through multiple JSON data in a jQuery response

https://i.stack.imgur.com/O0F6g.pngMy JSON response contains multiple data points presented as follows. WellNames […] 0 {…} id 56 well_name AL HALL api 34005205550000 1 {…} id 498 well_name BONTRAGER api 34005233850000 2 {…} id 499 ...

Attempting to modify a singular document within mongodb by leveraging node.js

const modifyTask = async (req, res) => { const { id } = req.params; let update = {}; if (req.body.taskTitle) update.taskTitle = req.body.taskTitle; if (req.body.taskContent) update.taskContent = req.body.taskContent; if (req.body.role) update. ...

When you add a new library using npm and it has a dependency on another existing library, it could potentially cause conflicts or issues with

After successfully installing a library, I am now looking to install another library that relies on the first one. I have some uncertainty about what will occur: The second library will utilize the shared library already installed for its functionality ...

Encountered an issue while attempting to load the required module

I'm having trouble setting up Stripe for my app and getting errors when trying to implement the module. Typically, I would require the module at the top of the file in order to use it, but when I do this in the paymentCtrl file, it doesn't work a ...

The error "navigator.permissions.query is not a defined object" is encountered in the evaluation

Whenever I try to access my website on an iPhone 7, I encounter a frustrating error. The main screen loads without any issues, but as soon as I click on something, a white bank screen appears. I believe this piece of code might be the cause: useEffect( ...

Unable to retrieve information for PayPal v2 Order generated on client side

My website features a membership option spread across 3 pages, each serving a specific purpose - one for presenting the membership along with a PayPal button, another for member registration into the database, and the last to inform users that they can now ...

The $digest function in Angular's $rootScope

While engrossed in the incredible book, Mastering Web Development in AngularJS, I stumbled upon this code snippet: var Restaurant = function ($q, $rootScope) { var currentOrder; this.takeOrder = function (orderedItems) { currentOrder = { deferred ...

Switching perspective in express with Pug

Hello everyone, I'm still getting the hang of using Node.js with Express and Jade. I've successfully set up a basic 1 page app where a login page is displayed by default. Once the user logs in and is authenticated against a database, the function ...