InterviewSolution
| 1. |
What is the data flow heeded by props, and what are the possible solutions for mutation cases? |
|
Answer» All props follow a ONE-way-down binding between the child's property and the PARENT one. i.e., when the parent property is updated, that latest prop value will be passed down to the child, but not the other way(child to parent, which is carried out by emitting events rather than using props). The child component should not mutate the prop defined in the parent; otherwise, it throws a warning in the console. The possible mutation cases can be solved in the ways illustrated below. The possible mutation cases are:
The solution for this mutation case is defining a local property in the child component and assigning the parent value as the initial value. The following code snippet clarifies the solution. props: ['defaultUser'],
The solution for this mutation case is defining a computed property using the prop's value. Computed properties are ANOTHER powerful feature from Vue that allows us to transform or perform calculations on our data and easily REUSE the result as an up-to-date variable in our template. Below is the illustration of computed properties to mutate the parent's prop props: ['environment'], computed: { localEnvironment: function () { return this.environment.trim().toUpperCase() } } |
|