Discover how a simple data pass can save you hours of debugging!
Why Props for parent to child data in Vue? - Purpose & Use Cases
Imagine you have a parent component with important data, and you want to show parts of it inside a child component. You try to copy and paste the data manually every time it changes.
Manually copying data is tiring and easy to forget. If the parent data changes, you must update the child data everywhere, which causes mistakes and confusion.
Props let the parent send data directly to the child in a clean, automatic way. When the parent data changes, the child updates itself without extra work.
const parentData = 'Hello';
let childData = parentData; // must update manually<ChildComponent :message="parentData" /> // child receives 'message' prop automatically
Props make it easy to share and keep data in sync between components, building clear and maintainable apps.
A shopping cart parent component sends the list of items to a child component that shows the item details. When the cart updates, the child updates automatically.
Manual data copying is slow and error-prone.
Props let parents send data to children automatically.
This keeps components in sync and code clean.
Practice
props in Vue components?Solution
Step 1: Understand the role of props
Props are designed to send data from a parent component down to its child components.Step 2: Differentiate props from other features
Local state stores data inside a component, events handle user actions, and methods define functions. Only props pass data downward.Final Answer:
To pass data from a parent component to a child component -> Option AQuick Check:
Props = Parent to Child Data [OK]
- Confusing props with local state
- Thinking props handle events
- Assuming props define methods
title in a Vue 3 child component using <script setup>?Solution
Step 1: Identify Vue 3
In Vue 3 with<script setup>syntax<script setup>, props are declared usingdefineProps().Step 2: Match the correct syntax for prop declaration
const props = defineProps({ title: String })correctly declares a prop namedtitleof type String.Final Answer:
const props = defineProps({ title: String }) -> Option AQuick Check:
Vue 3<script setup>uses defineProps() [OK]
- Using options API syntax inside
- Declaring props as local refs
- Forgetting to use defineProps() in
<ChildComponent :message="'Hello Vue'" />
And the child component script setup:
const props = defineProps({ message: String })
console.log(props.message)What will be logged to the console?
Solution
Step 1: Understand prop passing
The parent passes the string 'Hello Vue' as themessageprop to the child.Step 2: Accessing the prop in child
The child usesdefinePropsto declaremessageand logsprops.message, which holds 'Hello Vue'.Final Answer:
'Hello Vue' -> Option BQuick Check:
Passed prop value logs correctly [OK]
- Expecting props to be undefined without declaration
- Confusing read-only with inaccessible
- Assuming props are null if not reactive
<script setup>
const props = defineProps({ count: Number })
props.count = props.count + 1
</script>Solution
Step 1: Recall prop immutability
Props in Vue are read-only inside child components and cannot be directly modified.Step 2: Identify the error in code
The code tries to assign a new value toprops.count, which violates the read-only rule and causes an error.Final Answer:
Props are read-only and cannot be assigned new values -> Option DQuick Check:
Props cannot be changed inside child [OK]
- Trying to mutate props directly
- Confusing props with reactive state
- Ignoring Vue warnings about prop mutation
<script setup>?Parent template:
<UserList :users="['Alice', 'Bob', 'Charlie']" />
Choose the correct child component code:
Solution
Step 1: Check prop declaration type
The parent passes an array, so the child must declareusersas type Array usingdefineProps({ users: Array }).Step 2: Verify template usage
The child usesv-forto loop overprops.usersand display each user in a list item, which is correct.Final Answer:
The code using defineProps({ users: Array }) and v-for on props.users -> Option CQuick Check:
Array prop with defineProps and v-for works [OK]
- Declaring wrong prop type (e.g., String instead of Array)
- Not using defineProps in
- Trying to mutate props inside child
Ready to prove it?
Pass the test and this pattern moves unknown
