Runtime for Sveltes
The runtime side of Svelte covers how a component lives and shares data over time. Lifecycle functions such as onMount run code when a component appears on the page, and the context API lets a parent offer values to descendants via setContext and getContext without passing props through every level.
Stage: Component Patterns · Part of the Svelte roadmap · Last reviewed 19 November 2026
What to learn in Runtime
- Run browser-only code inside onMount and return a cleanup function
- Share values with deep descendants using setContext and getContext
- Explain why context is not reactive unless you put reactive state inside it
- Tidy up timers and listeners when a component is destroyed
- Wait for the DOM to update with tick
- Build shared reactive state in a .svelte.js module
Runtime interview questions
Why does onMount not run during server-side rendering?
onMount runs only in the browser after the component is attached to the DOM. During server rendering there is no DOM, so it is skipped. That makes it the right place for code that touches window, document or browser-only libraries.
When would you use context instead of props in Svelte?
Use context when many nested components need the same value, such as a theme or a form controller, and threading props through each layer would be noisy. For direct parent-child data, props remain clearer and easier to trace.
Where Runtime fits in the Svelte roadmap
Runtime belongs to the Component Patterns stage (intermediate level). Compose and share.
Other units in this stage
Back to the full Svelte roadmap