You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Due to the compositional nature of vuejs apps it is often necessary to define props that are simply passed down to some child component (without otherwise using them in the parent). By coercing an unset (= undefined) prop value to false, vue prevents the usage of default values in props with type Boolean. Also, in the context of Boolean typed props it does make a difference wether something is explicitly true or false or undefined.
Usage scenario
Suppose there are two components ParentComp and ChildComp.
ChildComp has props propA, propB and propC.
ParentComp uses an instance of ChildComp but it does not (and should not) know, what values to provide for the props of ChildProp
hence, ParentComp moves the responsibility of providing the ChildComp props out to the outer scope by itself defining props propA, propB and propC and simply passing them down to the child
now it's up to the user of ParentComp to provide the correct input for the props.
ChildComp has default values for it's own props
ParentComp does not define defaults. Remember, it does not know how to use ChildComp and simply moves this responsibility away from itself by letting the user decide
The setup described above leads to the following, useful behavior:
The user of ParentComp may decide to provide values for propA, propB and propCor may also ommit one or more of these props. If the values are ommited, the defaults of the child kick in and provide useful values.
The Problem
The bahavior described above works for all prop types except for Boolean. Ommiting parent prop values usually leads to the child prop using its own default values. In the case of Boolean props however, the parent prop without default value (= undefined) is always transformed to false, effectively breaking this pattern of use child default if parent prop value ommited. If the child defined a boolean prop with true as default, this value is now always overwritten by the parent prop to false. Now the parent prop has to explicitly define the prop default value to be true. In other words: It needs to know an implementation detail of it's child.
If you need other things than just Booleans, you should set a different type to the prop because that's the reality, you can have more than just booleans 🙂
What problem does this feature solve?
Due to the compositional nature of vuejs apps it is often necessary to define props that are simply passed down to some child component (without otherwise using them in the parent). By coercing an unset (= undefined) prop value to false, vue prevents the usage of default values in props with type Boolean. Also, in the context of Boolean typed props it does make a difference wether something is explicitly
true
orfalse
orundefined
.Usage scenario
ParentComp
andChildComp
.ChildComp
has propspropA
,propB
andpropC
.ParentComp
uses an instance ofChildComp
but it does not (and should not) know, what values to provide for the props of ChildPropParentComp
moves the responsibility of providing the ChildComp props out to the outer scope by itself defining propspropA
,propB
andpropC
and simply passing them down to the childParentComp
to provide the correct input for the props.ChildComp
has default values for it's own propsParentComp
does not define defaults. Remember, it does not know how to useChildComp
and simply moves this responsibility away from itself by letting the user decideThe setup described above leads to the following, useful behavior:
The user of
ParentComp
may decide to provide values forpropA
,propB
andpropC
or may also ommit one or more of these props. If the values are ommited, the defaults of the child kick in and provide useful values.The Problem
The bahavior described above works for all prop types except for
Boolean
. Ommiting parent prop values usually leads to the child prop using its own default values. In the case ofBoolean
props however, the parent prop without default value (= undefined) is always transformed tofalse
, effectively breaking this pattern of use child default if parent prop value ommited. If the child defined a boolean prop withtrue
as default, this value is now always overwritten by the parent prop tofalse
. Now the parent prop has to explicitly define the prop default value to betrue
. In other words: It needs to know an implementation detail of it's child.What does the proposed API look like?
ChildComp
props:ParentComp
props:ParentComp
template:Usage of
ParentComp
:Resulting values of
ChildComp
(as currently implemented in vue):propA: 'some default'
propB: []
propC: false
Proposed change:
propA: 'some default'
propB: []
propC: true
The text was updated successfully, but these errors were encountered: