BeginnerCoder

Lesson 19 — Provide / inject

Hand data to any descendant

provide(key, value) in a parent makes a value available to every component inside it, at any depth. A child reads it with inject(key). This skips prop drilling (passing a prop through components that do not need it). Note that a component cannot inject what it provides itself — it must be a child. If you provide a ref, children see its changes.

// parent:  provide("user", currentUser)
// any child: const user = inject("user")

Why it matters: themes, the logged-in user and app settings are shared this way.

Want it another way?
Task

Provide theme from the parent and inject it in ThemeLabel. It should show "Theme: dark", then "Theme: light" after Switch.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.

Keep going with Vue.js Foundations

Ready for more? Optional paid resources that pick up where this lesson leaves off.

Some links on this page are affiliate links: we may earn a commission at no extra cost to you. As an Amazon Associate I earn from qualifying purchases.