Vue.js Prop One-Way Data Flow
Last Updated :
22 Jul, 2024
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.
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 );
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.
Example 1: In this example, we get the props data from the parent, and use it as a local variable, and change some props value by local variables.
HTML
<!--App.vue-->
<template>
<h1>GeeksforGeeks</h1>
<Student Sname="Sam"
SAge="23"
SBatch="3rd"
SCourse="Mtech" />
</template>
<script>
import Student from './components/Student.vue';
export default {
name: 'App',
components: {
Student,
}
}
</script>
<style>
#app {
text-align: center;
margin-top: 60px;
}
h1 {
color: rgb(40, 212, 103)
}
</style>
HTML
<!--Student.vue-->
<template>
<h2>
Chick on botton to view the props
</h2>
<button v-on:click.left="temp = !temp">
Show
</button>
<h2 v-if="temp">
Name : {{ name }} <br />
Course : {{ Course }} <br />
Age : {{ Age }}<br />
Batch : {{ Batch }}<br />
<button @click="name='Satyam'">
change Name
</button>
</h2>
</template>
<script>
export default {
name: 'StudentC',
props: ['Sname', 'SAge', 'SCourse', 'SBatch'],
data() {
return {
name: this.Sname,
Age: this.SAge,
Batch: this.SBatch,
Course: this.SCourse,
temp: false
}
},
}
</script>
Output:
Example 2: In this example, we will get props from the user as raw data and perform some operations on values.
HTML
<!--App.vue-->
<template>
<h1>GeeksforGeeks</h1>
<Student Sname="Sam Snehil"
Age="23"
SCourse="Btech"
SBatch="3rd" />
</template>
<script>
import Student from './components/Student.vue';
export default {
name: 'App',
components: {
Student,
}
}
</script>
<style>
#app {
text-align: center;
margin-top: 60px;
}
h1 {
color: rgb(40, 212, 103)
}
</style>
HTML
<!--Student.vue-->
<template>
<h2>
Chick on botton to view the props
</h2>
<button v-on:click.left="temp = !temp">
Show
</button>
<h2 v-if="temp">
Name : {{ name }}<br />
Course : {{ course }} <br />
Age : {{ Age }}<br />
Batch : {{ batch }}<br />
<button @click="name='Satyam'">
change Name
</button>
</h2>
</template>
<script>
export default {
name: 'StudentC',
props: ['Sname', 'Age', 'SCourse', 'SBatch'],
computed: {
name: function () {
return this.Sname.toUpperCase();
},
course: function () {
return this.SCourse.toUpperCase()
},
batch: function () {
return this.SBatch.toUpperCase()
},
},
data() {
return { temp: false };
}
}
</script>
Output:
Reference: https://vuejs.org/guide/components/props.html#one-way-data-flow
Similar Reads
Props vs Data in Vue.js Props in Vue.jsProps in Vue.js are custom attributes that allow data to be passed from a parent component to a child component. This enables the parent component to communicate with its child components, providing them with data or functionality. In Vue.js, props are used to pass data from parent co
3 min read
Vue.js Prop Passing Details Vue.js Props are used to pass data to the component. For passing details to props we have to first declare props. To declare the prop name we choose a name that is more descriptive of the value that it holds. In this article, we will see the Prop passing the details in Vue.js. The following are the
4 min read
How to Properly Watch for Nested Data in VueJS ? In Vue.js, we can watch the nested data to properly control the changes within the nested properties. In this article, we will explore both of the approaches with the practical implementation of these approaches in terms of examples and outputs. We can watch this by using two different approaches: T
3 min read
Vue.js Props Declaration Vue.js Props Declaration facilitates the declaration of props explicitly for the Vue components. With this, Vue can identify how external props are passed to the component that should be treated as fall-through attributes. In other words, Props of Vue components is a configuration for the transfer o
3 min read
Vue.js Two Way Binding Model Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor
2 min read
How a View-model works in Vue.js? Vue.js is a front-end progressive javascript framework for building User Interfaces ( UI ) and Single Page Applications. This framework focuses on Model â View â ViewModel architecture pattern that connects the View ( User Interface ) and the Model ( Data Objects ). Vue.js uses many built-in directi
4 min read
How to catch props data on component change in Vue.js ? In this article, we will see how we can pass different data values between components with the help of props in Vue.js along with a basic understanding of props, components, and their syntax with an example. Vue.js is an open-source progressive JavaScript framework for developing user interfaces and
4 min read
Vue.js Props Vue.js Props is an attribute to the configuration of the component. It is used to pass the data to the component to work dynamically. The props configuration defines the type of data it can receive from different components of Vue.Table of ContentProps DeclarationProp Passing DetailsOne-Way Data Flo
4 min read
Vue.js v-once Directive The v-once directive is a Vue.js directive that is used to avoid unwanted re-renders of an element. It treats the element as a static content after rendering it once for the first time. This improves performance as it does not have to be rendered again. First, we will create a div element with the i
1 min read
How to Call a Vue.js Function on Page Load ? In this article, we will learn how to call a vue.js function on page load in VueJS. We will explore two different approaches with practical implementation in terms of examples and outputs. Table of Content Using mounted HookUsing created HookUsing mounted HookIn this approach, are using the mounted
2 min read