Open code, practice, and notes →
Download source with English comments · for reading
Svelte · 17 Connecting Workspaces with Context
Series contents · Technology map
Stage: Stores and Workspace State · External example · preview instructions · Storage ID: svelte-step-17
Goals and learning outcomes
- The latest string appears consistently within each tree.
- Drafts in the other tree do not change.
Prerequisites
Unit 17 continues with the work and experiment records from Unit 16.
Key concepts and explanations
Context
setContext and getContext pass dependencies within a component tree. Unlike a single global store, they can provide a separate scope for each tree.
Reactive Objects
When you pass a reactive object through Context, consumers read that object. Decide how to preserve the shared contract instead of replacing the entire reference.
Practice setup
First check the execution scope guidance on the example screen. The following environment is for extending the assignment in a separate project. Compile and run .svelte files in a separate Svelte 5 Vite project. Record package versions and the lockfile. Frontory’s Svelte preview uses actual writable stores and an explicit DOM adapter; it is not a .svelte compiler or a runes runner.
The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.
Practice tasks
- In src/Workspace.svelte, use setContext with a Symbol key to provide the notes object and update function.
- Read them with getContext in a deeply nested NoteEditor.svelte.
- Edit the same id in two nested Workspaces and check isolation.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| Provider | 1 shared provider | 2 nested providers | The inner editor reads the draft from the nearest workspace. |
Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.
Completion criteria
- The latest string appears consistently within each tree.
- Drafts in the other tree do not change.
Self-check
- Why is retrieving Context outside initialization a problem?
- How do you avoid string key collisions?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
The preview is a Svelte writable/DOM contract experiment. To run runes, snippets, and compiled output, run the actual App.svelte in a Svelte 5 project.
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: Shared Runes Modules · Next: Requests and Stale Results