0
0
Vueframework~5 mins

Props drilling problem in Vue

Choose your learning style9 modes available
Introduction

Props drilling happens when you pass data through many layers of components just to reach the one that needs it. It can make your code messy and hard to manage.

You have a parent component with data that a deeply nested child needs.
You want to share a value or function from a top-level component to a lower-level one.
You notice many intermediate components only pass props without using them.
You want to understand why your component tree feels complicated to update.
Syntax
Vue
<ChildComponent :someProp="someValue" />
Props are how parent components send data to child components in Vue.
When many components pass props down just to reach a distant child, it is called props drilling.
Examples
Parent passes 'info' to IntermediateComponent as 'data'.
Vue
<ParentComponent>
  <IntermediateComponent :data="info" />
</ParentComponent>
IntermediateComponent passes the same 'data' prop further down.
Vue
<IntermediateComponent>
  <DeepChildComponent :data="data" />
</IntermediateComponent>
DeepChildComponent finally uses the 'data' prop.
Vue
<DeepChildComponent>
  <p>{{ data }}</p>
</DeepChildComponent>
Sample Program

This example shows props drilling: the 'info' string is passed from ParentComponent to IntermediateComponent, then to DeepChildComponent, which displays it.

Vue
<template>
  <ParentComponent />
</template>

<script setup>
import { defineComponent } from 'vue'

const DeepChildComponent = defineComponent({
  props: { data: String },
  template: `<p>Data received: {{ data }}</p>`
})

const IntermediateComponent = defineComponent({
  props: { data: String },
  components: { DeepChildComponent },
  template: `<DeepChildComponent :data="data" />`
})

const ParentComponent = defineComponent({
  components: { IntermediateComponent },
  data() {
    return { info: 'Hello from Parent!' }
  },
  template: `<IntermediateComponent :data="info" />`
})
</script>
OutputSuccess
Important Notes

Props drilling can make components tightly linked and harder to change.

Vue offers other ways like provide/inject or state management to avoid props drilling.

Summary

Props drilling means passing props through many layers to reach a child.

It can make your code harder to maintain and understand.

Look for ways to reduce props drilling for cleaner code.