Issue with child prop not being updated despite changes in parent component

I'm facing a strange issue where altering a child component's prop doesn't trigger a re-render.

Here's the scenario: In the parent component, I have:

<child :problemProp="proplemPropValue"></child>

In the child component, I've defined the prop as:

{
    name: "Child",
    props: {
    problemProp: {
        type: [File],
        required: true
    }
}

When trying to render it in the child component:

<template>
  <div id="dropzone-preview" class="file-row">
    {{problemProp}} <--This should display the prop as a JSON string-->
  </div>
</template>

In the initial rendering, everything displays correctly. However, when a property within problemProp, such as upload.progress, changes in the parent component, it doesn't reflect in the child component.

If I introduce a new prop, dummyProp, in the child component:

   {
    name: "Child",
    props: {
    problemProp: {
        type: [File],
        required: true
    },
    dummyProp: {
        type: Number
    }
}

Surprisingly, changing dummyProp now causes problemProp to update as well. What could be causing this behavior? Why does altering dummyProp trigger a re-render but changes to problemProp do not?

Answer №1

The main issue seems to stem from the fact that Vue fails to transform a File object into an observed value when added to an array. This is because Vue disregards browser API objects:

The object must be plain: native objects such as browser API objects and prototype properties are ignored.

As a result, any modifications made to that object will not automatically reflect in Vue (as expected), since Vue remains unaware of these changes. This explains why updating dummyProp seems to work, as changes to this property are indeed observed and prompt a re-render.

So, what can be done? I managed to make your bin function by slightly altering the addedFile method.

addedfile(file){
  console.log(file);
  self.problemPropValues.push(Object.assign({}, file));
},

Firstly, there's no need (or benefit) in using $data; simply refer directly to the property. Secondly, by creating a copy via Object.assign, Vue effectively observes the object, resulting in updates being reflected in the child component. Depending on your requirements, you may eventually require a deep copy method instead of Object.assign, but for now this solution appears to be effective.

Here is the modified code snippet that successfully got the bin working (transferred to CodePen for ease of use).

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

Issue with IE7 Dropdownlist not appearing after changing class name when onFocus event is triggered for the first time

I need to adjust the CSS class of a dropdownlist when it is focused on, in order to change its background color. However, in IE7, when the dropdownlist is clicked, the CSS class changes but the options list does not appear until the dropdownlist is clicke ...

Are you using yeoman with csslint in your project?

As a newcomer to grunt, I am facing challenges in integrating csslint to monitor my project while running "grunt serve" for csslinting. I am specifically struggling with disabling the adjoining classes warning since it is not relevant for IE6 compatibili ...

What is the best way to collect and store data from various sources in an HTML interface to a Google Spreadsheet?

Currently, I have a spreadsheet with a button that is supposed to link to a function in my Google Apps Script called openInputDialog. The goal is for this button to open an HTML UI where users can input text into five fields. This input should then be adde ...

Shutting down the jQuery pop-up

I am struggling with trying to display this code as a popup on my website. Here is the code I have: <div id="myDialog" title="myTitle"> <div class="table_cell"> <div class="message"></div> </div> <div class="tabl ...

Is there a way for me to display an alert notification when a website requests access to additional storage on my computer?

Is there a way to set up an alert notification in Internet Explorer when a website requests additional storage on your computer? The notification would ask: Do you want to grant permission for this website to use additional storage on your computer? ...

Ajax sends the URL location to Python

I'm attempting to piece together some code. There are two distinct functions that I am trying to merge into a single entity. Code snippet: <!DOCTYPE html> <head> <meta http-equiv="content-type" content="text/html;charset=UTF-8"> &l ...

Setting up the ajax header and implementing a redirect

I have a simple node authentication app. My goal is to send a token in the header when redirecting to a URL. For example, when a user clicks on their profile, they should be redirected with a token so they can access that page. I've tried implementin ...

Attempting to create a multi-page slider using a combination of CSS and JavaScript

Looking for help with creating a slider effect that is triggered when navigating to page 2. Ideally, the div should expand with a width of 200% or similar. Any assistance would be greatly appreciated! http://jsfiddle.net/juxzg6fn/ <html> <head& ...

Creating a unique-looking visual representation of progress with arcs

Looking to create a circular progress bar (see image below), with loading starting from the left bottom side up to the right bottom side. The empty state should be light-blue (#E8F6FD) and the progress color strong blue (#1CADEB). I've experimented w ...

Scope binding is successful, but accessing the array is only possible after adding an Alert() function

Within my Angular controller, I'm utilizing the SharePoint JavaScript Object Model to fetch data from the Taxonomy (term store). Due to SharePoint's JSOM not being a conventional Angular function that can be easily understood by the scope, I util ...

Modifying dat.gui to reflect changes made to the selected object

check out this working code I am currently exploring ways to update or refresh dat.gui in order to reflect the changes I make to my selection. My main objective is to generate random cubes and then manipulate a single cube by rotating, scaling, changing p ...

There was an unexpected error encountered while trying to use Jade

I encountered an error in my jade template: Error: E:\Do\hello_express\node_notes\views\simple.jade:6 4| meta(charset="utf-8") 5| meta(name="viewport",content="width=device-width,initial-scale=1,maximum-scal ...

What are the steps to manipulate the data within an EJS file after passing an array through Node.js as an object?

I'm currently developing a simple calendar application using Node.js and ejs. I am working on passing an array of months through express to my ejs file, with the goal of being able to cycle through each month by clicking the next button. How can I imp ...

Incorporating a YouTube channel into mobile websites

While it's relatively easy to embed single YouTube videos in mobile pages with the help of Google, I'm still struggling with embedding a whole channel. The issue seems to revolve around the screen width, and my attempts at using JavaScript have n ...

When using React, it's important to verify if the page has been refreshed before updating the local storage based on the GraphQL query. If the page

Currently, I am working on implementing a hit counter feature that increments each time a user clicks a button. The setup involves GatsbyJS with React and utilizes a lambda function to store the count in FaunaDB. Additionally, the client-side functionality ...

JavaScript regular expression for detecting valid characters without repeating a specific character

let rx = /(\/MxML\/[a-z0-9\[\]@/]*)/gi; let s = 'If (/MxML/trades[1]/value== 1 then /MxML/trades[type=2]/value must be /MxML/stream/pre/reference@href'; let m; let res = []; while ((m = rx.exec(s))) { res.push(m[1]); ...

retrieve content within an iframe via ajax

I currently have an iframe set up on the server side like this: <iframe frameborder="0" runat="server" style="width: 100%; height: 700px; background-color: #bacad3;" id="I1" name="I1" src="Page.aspx"></iframe> and I update the content dynamic ...

Incorporating Vue 3 with Transition and KeepAlive nested within RouterView

I recently upgraded my application from Vue 2 to Vue 3, but I am experiencing issues with the KeepAlive feature. Despite attempting to navigate back to a previous page included in the KeepAlive, the state does not seem to be cached. Has anyone been able ...

The condition of the variable not being bound

<button id="btn1"> Button 1 </button> <button id="btn2" v-if="state == 2"> Button 2 </button> export default { data() { return { emailPreferences: { isLoading: false ...

Is there an issue with the JSON data?

"stocksdata" :[{"id":7,"SCRIP":"ASIANPAINT","LTP":3341,"OHL":"BUY","ORB15":"BREAKOUT","ORB30":"NT","PRB":"NA","CAMARILLA& ...