Recently, I came across an interesting observation regarding prop mutation in Vue 2.6.
It is generally advised to avoid mutating props directly in a child component as it can lead to the well-known warning:
"Avoid mutating a prop directly since the value will be overwritten...."
For example, if I have a parent component with data like this:
example: "Hello World"
and I pass it down as a prop to a child component. If I try to mutate it in the child component like so:
this.example = "Hello World!"
I would receive a warning. However, I noticed that if the data in the parent component is an object like:
example : {message : "Hello World"}
and in the child component I do this:
this.example.message = "Hello World!"
No warning is triggered and the data in the parent component gets updated. This made me wonder why prop mutation propagates to the parent in one case but not in the other. Could it be related to how JavaScript stores these variables? Is it considered good practice to utilize this behavior?