Vue Academy · Lesson

provide() in Parent Components

Providing primitive values, reactive refs, and objects — maintaining reactivity through provide.

Lesson 2 of 413 steps

provide() in Parent Components is a free Vue Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, and your progress syncs across the web and the CoddyKit app. The Vue Academy course includes 4 lessons in total.

The provide Function

Vue provide lets a component offer data to all of its descendants. Call it in <script setup> with a key and a value.

import { provide } from "vue";
provide("message", "Hello from ancestor");

Any Descendant Can Inject

Once provided, any component anywhere below in the tree can inject the same key - no matter how many layers deep - without intermediate components doing anything.

App (provides "message")
  -> ... -> DeepChild (injects "message")

Providing Reactive State

To keep descendants reactive, provide a ref (or reactive object) rather than a plain value. Updates to the ref propagate to every consumer automatically.

import { provide, ref } from "vue";
const theme = ref("dark");
provide("theme", theme);

Reactivity Flows Down

When the ancestor changes theme.value, every descendant that injected it re-renders with the new value. This is the key advantage over passing a static snapshot.

function toggle() {
  theme.value = theme.value === "dark" ? "light" : "dark";
}

Protecting State with readonly

By default a descendant could mutate the injected ref, which scatters state changes and breaks one-way data flow. Wrap the value in readonly() so consumers can read but not write.

import { provide, ref, readonly } from "vue";
const user = ref({ name: "Ada" });
provide("user", readonly(user));

Providing Functions as Actions

To let descendants request changes, provide a function alongside the readonly state. Consumers call the function; only the provider mutates the source.

function updateName(name) {
  user.value.name = name;
}
provide("user", readonly(user));
provide("updateName", updateName);

Bundling State and Actions

A clean pattern is to provide a single object with the readonly state and its actions, similar to a tiny store.

provide("userStore", {
  user: readonly(user),
  updateName
});

App-Level provide

You can also provide at the application level so the value is available to every component, set up once on the app instance. (Covered in detail later.)

app.provide("appName", "CoddyKit");

Symbol Keys to Avoid Collisions

String keys can clash in large apps. Use a Symbol exported from a shared module as the key for guaranteed uniqueness.

// keys.js
export const themeKey = Symbol("theme");
// provider:
provide(themeKey, theme);

When to Use provide

Provide is ideal for theme, current user, i18n, form context, or feature configuration - values needed by a subtree. For one or two levels, plain props are still simpler.

Provider Scope

Provided values are scoped to the providing component subtree. Two sibling subtrees can provide different values under the same key without interfering.

Quick Check

How do you stop descendants from mutating provided state?

Recap

You learned provide(key, value) exposes data to all descendants. Provide a ref to keep reactivity, wrap it in readonly() to prevent mutation, supply functions as actions, and prefer Symbol keys to avoid collisions.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
42
Lessons
156

Frequently Asked Questions

Is the “provide() in Parent Components” lesson free?

Yes — any 3 lessons of the Vue Academy learning path, including “provide() in Parent Components”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Vue Academy course includes 4 lessons in total.

What will I learn in “provide() in Parent Components”?

Providing primitive values, reactive refs, and objects — maintaining reactivity through provide. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Vue Academy?

No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “provide() in Parent Components” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Vue Academy lesson?

Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Prop Drilling Problem
  2. provide() in Parent Components
  3. inject() in Child Components
  4. Provide/Inject for App-Level State
← Back to Vue Academy