provide() in Parent Components
Providing primitive values, reactive refs, and objects — maintaining reactivity through provide.
provide() in Parent Components is a free Vue Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “provide() in Parent Components” lesson free?
Yes — the full text of “provide() in Parent Components” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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
- The Prop Drilling Problem
- provide() in Parent Components
- inject() in Child Components
- Provide/Inject for App-Level State