setContext() and getContext()
Provide and consume context values scoped to a component subtree.
setContext() and getContext() is a free Sveltejs Academy lesson on CoddyKit — lesson 1 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 Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is Context
Context lets a parent component pass data to all its descendants without prop drilling.
Import Helpers
From svelte.
import { setContext, getContext } from "svelte";Set in Parent
Call setContext(key, value) in the parent script.
setContext("theme", { color: "dark" });Get in Descendant
Call getContext(key) in any descendant to read it.
const theme = getContext("theme");Keys
Keys can be strings or symbols. Symbols avoid collisions.
const themeKey = Symbol("theme");Snapshots
The value passed to setContext is captured once. Use stores for reactive values.
Reactive Context
Pass a store as context to enable reactive updates throughout descendants.
setContext("user", userStore);Scoped to Subtree
Context is only available to descendants of the component that set it.
Multiple Contexts
Each component can set multiple contexts; descendants can read any by key.
Use Cases
Theme, i18n, auth, dependency injection. Anything that flows down without being passed prop by prop.
Compared to Stores
Stores are global; context is scoped to the component tree where setContext was called.
Quick Check
Who can read a context?
Recap
Use setContext(key, val) and getContext(key) to share data through the component tree without props.
Frequently asked questions
Is the “setContext() and getContext()” lesson free?
Yes — the full text of “setContext() and getContext()” is free to read here on the web, and the Sveltejs 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 Sveltejs Academy course, upgrade to CoddyKit PRO.
What will I learn in “setContext() and getContext()”?
Provide and consume context values scoped to a component subtree. You practise Sveltejs 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 Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “setContext() and getContext()” 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 Sveltejs Academy lesson?
Yes. Every Sveltejs 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
- setContext() and getContext()
- Context vs Stores: When to Use Each
- Typed Context with TypeScript
- Context for Dependency Injection