Using setContext in Svelte to Share Data
📖 Scenario: You are building a simple Svelte app where a parent component shares a message with its child component using setContext and getContext.
🎯 Goal: Create a parent component that uses setContext to provide a message string. Then create a child component that uses getContext to receive and display that message.
📋 What You'll Learn
Create a parent Svelte component with a message string
Use
setContext in the parent to share the messageCreate a child Svelte component
Use
getContext in the child to access the messageDisplay the message inside the child component
💡 Why This Matters
🌍 Real World
Sharing data between components without passing props directly is common in apps with nested components, like theme settings or user info.
💼 Career
Understanding setContext and getContext is important for building scalable Svelte apps where components need to share data cleanly.
Progress0 / 4 steps