Concept Flow - Props for parent to child data
Parent Component
Child Component
Use props in template or script
Render child with passed data
The parent sends data to the child using props. The child receives and uses this data to render or compute.
<template> <ChildComponent :message="parentMessage" /> </template> <script setup> const parentMessage = 'Hello from Parent' </script>
| Step | Action | Parent Data | Prop Passed | Child Receives | Child Renders |
|---|---|---|---|---|---|
| 1 | Parent defines 'parentMessage' = 'Hello from Parent' | parentMessage = 'Hello from Parent' | N/A | N/A | N/A |
| 2 | Parent renders <ChildComponent :message='parentMessage' /> | parentMessage = 'Hello from Parent' | message = 'Hello from Parent' | N/A | N/A |
| 3 | Child receives prop 'message' | parentMessage = 'Hello from Parent' | message = 'Hello from Parent' | message = 'Hello from Parent' | N/A |
| 4 | Child renders template using 'message' | parentMessage = 'Hello from Parent' | message = 'Hello from Parent' | message = 'Hello from Parent' | Displays: Hello from Parent |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| parentMessage | undefined | 'Hello from Parent' | 'Hello from Parent' | 'Hello from Parent' | 'Hello from Parent' |
| message (prop in child) | undefined | undefined | 'Hello from Parent' | 'Hello from Parent' | 'Hello from Parent' |
Props let a parent send data to a child component. Parent passes data using :propName="value" syntax. Child declares props to receive them. Child uses props in template or script. Props are reactive and update child on change.
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