Vue.js Developer interview question · Most asked
How do props work, and what is one-way data flow?
Summary
Props pass data from parent to child; when the parent's value changes, the child receives the update. The child must not mutate a prop: data flows down, and changes go back up as events, so the parent stays the owner of its state.
Longer Answer
Declare props with defineProps (with types and defaults, or TypeScript generics). If the child needs a local, editable copy, initialise a ref from the prop; if it needs a transformed value, use a computed. Mutating a primitive prop triggers a warning; mutating a property of an object prop does not warn (objects are shared by reference) but is still bad practice, because the parent's state changes behind its back.
Since Vue 3.5, destructuring defineProps() keeps reactivity in <script setup> and supports default values with plain JavaScript defaults.
Key Points
- Parent to child, read-only in the child.
- Changes go up via events.
- Local copy (ref) or derived value (computed).
- Object props are shared references.
Deep dive: Code Clinic: review a Vue comment list
More most-asked Vue.js Developer questions
- Options API or Composition API: what is the difference, and which would you use?
- What are composables, and why did they replace mixins?
- How does Vue 3's reactivity system work, and how does it differ from Vue 2's?
- computed, watch or watchEffect: when do you use each?
- ref or reactive: what is the difference, and when do you use each?
- How do you use Vue Router: dynamic routes, nested routes and navigation guards?
- Why does v-for need a key, and why should it not be the index?
- Pinia or Vuex: what is the difference, and why is Pinia now recommended?