Vue support both primitive types and object as props to pass them from parent to its child.
I ever heared that it is a best practice to always pass primitive types instead of passing an Object. Maybe it is because the primitive types are easy to detect if changed.
Is it true? Is it a best practice or just something dumb?
All props are optional by default, unless required: true is specified. An absent optional prop other than Boolean will have undefined value. The Boolean absent props will be cast to false .
In summary, we use props to pass down data from the parent components to the child component(s). The child component also emit events to the parent component(s) in case you need to send data/events from the child to the parent component.
Props and data are both reactiveWith Vue you don't need to think all that much about when the component will update itself and render new changes to the screen. This is because Vue is reactive. Instead of calling setState every time you want to change something, you just change the thing!
Vue components are written as a combination of JavaScript objects that manage the app's data and an HTML-based template syntax that maps to the underlying DOM structure.
There is no real "best practice" at it really depends on what you're trying to accomplish.
You can use both really, but remember when passing non-primitives you're passing a POINTER, not the actual object. Thus, when modifying said object inside the child, you will also modify the original object.
If you're going to pass around objects, that you want to MODIFY, but as a "copy", you can always pass them using the expand operator to create a copy as such.
{ ...myObject }
[ ...myArray ]
<child-object :someprop="{...object}"></child-object>
That way you ensure that if you're going to modify the object at child level, the child owns a copy of this object and you're not getting unexpected behavior on the parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With