Is it possible for a prop to change dynamically?

I am currently developing a component that is responsible for receiving data through a prop, making modifications to that data, and then emitting it back to the parent (as well as watching for changes).

Is it possible for a prop to be reactive? If not, what is the alternative method for assigning a prop to a reactive property?

I attempted to address this issue by:

  • defining props: ["receiveddata"]
  • creating a reactive property within data (for example, hello: '')
  • setting this.hello = this.receiveddata in the mounted() lifecycle hook

My expectation was that at this stage, this.hello would contain the content passed via the receiveddata prop, allowing me to work on this.hello thereafter (modifying it, monitoring it, and emitting it when necessary)

However, upon inspecting the values using DevTools, I observed that

  • this.hello was undefined
  • this.receiveddata correctly stored the initially provided data

It seems that during the mounted() phase, the prop values are not yet available (explaining the initial undefined value of

this.receiveddata</code), which is then assigned to <code>this.hello
, only to be populated later when this.receiveddata is updated.

In essence, my assignment of this.hello = this.receiveddata was incorrectly placed. However, finding the correct placement remains a challenge (I have even experimented with switching from mounted() to created())

Answer №1

It seems like you're looking for a way to handle this situation, and one approach I usually take is using a computed property with a getter and setter.

props: {
  someProp: String,
},
computed: {
  somePropLocal: {
    get () {
      return this.someProp
    },
    set (value) {
      this.$emit('input', value)
    },
  },
}

By setting it up this way, you can manipulate the value freely while ensuring that the correct value will be emitted.

this.somePropLocal = 'Whatever'

Additionally, if the parent directly modifies somePropLocal, it will be automatically updated as well.

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 steps should I take to address the node-gyp version issue within the Coreui Project?

I'm facing an issue with my vue/coreui project as I try to install node packages like npm, node-sass, and node-gyp. However, during the installation process, I encountered the following error message. Our setup also includes python3.11, which might be ...

Is it possible to utilize the same Context in multiple forms within React?

Is it possible to utilize React Context to store the state of multiple forms, or should each form have its own unique Context? Below is a snippet of code that showcases the use of React Hooks API: In FormContext.js ... import {FormReducer} from './ ...

Using Python's Requests library to authenticate on a website using an AJAX JSON POST request

I'm a beginner in Python and struggling to create the correct code for using Python requests to log in to a website. Here is the form code from the website: <form autocomplete="off" class="js-loginFormModal"> <input type="hidden ...

Unable to load component - utilizing vue-airbnb-style-datepicker

Attempting to use the vue-airbnb-style-datepicker Added to my file.vue: import AirbnbStyleDatepicker from 'vue-airbnb-style-datepicker' import format from 'date-fns/format' export default { components: { AirbnbStyleDatepicker ...

Using Vuex to import a specific state value from an object

I have a simple shop: CustomersStore.js state() { return { customers: { name: '', customerHasPermissions: false, } } } I am attempting to use mapState to access the state in my component. When I import th ...

The React Component is limited to updating once

Exploring the React Native code below: import React from 'react'; import {Text, View, StyleSheet, Button} from 'react-native'; export default class Target extends React.Component { constructor(props){ super(props); ...

How can I fetch data from SQL using JavaScript based on a specific value in PHP?

My application is built using the Yii2 framework. Within my application, there is a view.php file that consists of an element and a button. The element, <div id="userId">, contains the user's login ID, and I aim to use the button to re ...

Visual Studio Code is not properly highlighting imports for Vue 3 using the Composition API

Upon careful examination, it is evident that the import is not highlighted despite the utilization of the component SearchBar. https://i.stack.imgur.com/G4sAm.png Various attempts have been made to rectify this issue including the installation of the Vet ...

Is it possible to pass an AngularJS ng-form object as a parameter in ng-if?

When I try to preview, the save button in my preview mode remains enabled. You can view the code snippet here: http://plnkr.co/edit/I3n29LHP2Yotiw8vkW0i I believe this issue arises because the form object (testAddForm) is not accessible within the ng-if s ...

Adjust size of element in relation to Background Cover using jQuery

Can you help me solve a challenge I'm facing? I have a website with a fullsize background image, and I need to attach a div to a specific position on the image while ensuring that it scales in the same way as the background image with the "background ...

Uncertain about how to transfer data between server and client in Next.js?

I'm currently grappling with understanding the proper method of exchanging data between server-side and client-side components in NextJS 13. To simplify my learning process, I have created a basic scenario. In this setup, I have two functions that pe ...

Differences in HTML animations can be seen when comparing Google Chrome to Microsoft Edge. Looking for a workaround for autoplay to ensure

My intro video animation is facing recording difficulties due to the autoplay policy in Google Chrome. It seems nearly impossible to capture it accurately. If only autoplay could function for an offline HTML file, my issue would be resolved. Unfortunately ...

The back button fails to refresh the page on a website utilizing Ajax technology

Recently, I implemented AJAX on a client's website to introduce some smooth animations for page transitions. When navigating from the homepage of firedogcreative.com to the edit page, and then to one of the work pages, we see a history like this: fir ...

The onChange Event triggers only once in a checkbox input

I am implementing a checkbox component that emits an event to the parent component. However, in the parent component, I am facing an issue where I need to perform different actions based on whether the checkbox is checked or not, but it seems to only work ...

Is there a way to display the form values on the table once it has been submitted?

I'm struggling with my form input not showing up under the table headings after submission. I've reviewed the code multiple times, but can't figure out what's causing the issue. If you have any suggestions on how to write the code more ...

Headers cannot be sent to the client after they have already been set in Axios within Next.js

For additional discussion on this issue, please refer to the GitHub thread at - https://github.com/axios/axios/issues/2743 In my Next.js project, I am using Axios and occasionally encounter an error related to interceptors when returning a Promise.reject. ...

Problem with HTML relative paths when linking script sources

Having trouble with a website I constructed using Angular. The problem lies in the references to javascript files in index.html. The issue is that the paths in the HTML are relative to the file, but the browser is searching for the files in the root direct ...

The setInterval function will run just one time when triggered by an onclick event

After watching a YouTube tutorial on creating desktop notifications, I had an idea to use it as a reminder tool. For example, having a specific reminder appear as a desktop notification every 30 minutes when triggered by clicking a button. However, the cod ...

What is causing the "unable to resolve dependency tree" error when using ng new newApp?

Struggling with creating a new Angular app using the command ng new app-name? When running the command, you may encounter the following error in the command line. Installing packages (npm)...npm ERR! code ERESOLVE npm ERR! ERESOLVE unable to resolve depen ...

"Regarding compatibility with different browsers - IE8, Firefox3.6, and Chrome: An inquiry on

Snippet of JavaScript Code: var httprequest = new XMLHttpRequest(); var time = new Date(); if (httprequest) { httprequest.onreadystatechange = function () { if (httprequest.readyState == 4) { alert("OK"); } }; httprequest.open("GET", ...