I'm a bit confused about how to change properties inside components, let's say I have the following component:
{
props: {
visible: {
type: Boolean,
default: true
}
},
methods: {
hide() {
this.visible = false;
}
}
}
Although it works, it would give the following warning:
Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "visible" (found in component )
Now I'm wondering what the best way to handle this is, obviously the visible
property is passed in when created the component in the DOM: <Foo :visible="false"></Foo>
hide()
could be on parent which then sends a reference to it as a prop). – ArneHugovisible
(boolean) andhide
(function). Thenhide
is defined on the parent, which also owns the state ofvisible
. That way you don't edit props, but you edit the parent state, which is allowed. – ArneHugo