Different Cases where the Child Component tries to change Props

The following are the cases where the child component tries to modify the Props:

  • When the Child component gets props from the parent and wants to use it as a local data property. In such a case, we can use a local variable of the child component that is initialized with props value.
props = defineProps( ['props1'] );
// Variable intialize with props value
temp = props1;
  • When we need some props that are not in the final data, it is raw data for final data. In such a case, we can use computed property with the prop’s value.
props = defineProps( ['firstName', 'lastName' ]);
// Applying computed property on props
fullName = computed( () => firstName + lastName );

Vue.js Prop One-Way Data Flow

In VueJS, all the props are bonded between the child and properties is one-way-down. When we update the prop in the parent component, it is updated to all the child components, but the reverse does not happen and VueJS warns the use. This is used as protection from altering the state of a parent-by-child component.

Similar Reads

Different Cases where the Child Component tries to change Props

The following are the cases where the child component tries to modify the Props:...

Mutating Object / Array Props

One-way data binding only prevents the String, Number, and Boolean, but, it does not prevent the mutation of array and object. Vue.js does not prevent the array and object mutation because the array and object are passed as a reference to the child and prevention is expensive as compared to others. Such mutation affects the parent state and makes the process of understanding the flow of data between components more difficult. We should allow such mutation only when parent and child are tightly bound or the child should trigger an event that tells the parent to change props data....