Vue best practice, props object or primitive types?

Viewed 1851

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?

1 Answers

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.

Related