Performance: Props for parent to child data
This affects the rendering speed and responsiveness by controlling how data flows from parent to child components in Vue.
<template> <ChildComponent :a="complexObject.a" /> </template> <script setup> import { reactive } from 'vue' const complexObject = reactive({ a: 1, b: 2, c: 3 }) </script>
<template> <ChildComponent :data="complexObject" /> </template> <script setup> import { reactive } from 'vue' const complexObject = reactive({ a: 1, b: 2, c: 3 }) </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing large reactive object as prop | High (many reactive dependencies) | Multiple reflows on any change | High paint cost due to frequent updates | [X] Bad |
| Passing only needed primitive props | Low (minimal reactive dependencies) | Single reflow on prop change | Low paint cost with fewer updates | [OK] Good |
props in Vue components?title in a Vue 3 child component using <script setup>?<script setup> syntax<script setup>, props are declared using defineProps().const props = defineProps({ title: String }) correctly declares a prop named title of type String.<script setup> uses defineProps() [OK]<ChildComponent :message="'Hello Vue'" />
const props = defineProps({ message: String })
console.log(props.message)message prop to the child.defineProps to declare message and logs props.message, which holds 'Hello Vue'.<script setup>
const props = defineProps({ count: Number })
props.count = props.count + 1
</script>props.count, which violates the read-only rule and causes an error.<script setup>?<UserList :users="['Alice', 'Bob', 'Charlie']" />
users as type Array using defineProps({ users: Array }).v-for to loop over props.users and display each user in a list item, which is correct.Pass the test and this pattern moves unknown