Context vs Stores Decision in Svelte
📖 Scenario: You are building a simple Svelte app that shares a user's theme preference (light or dark) across components.You want to learn when to use context and when to use stores in Svelte for sharing data.
🎯 Goal: Create a Svelte app that sets up a theme preference using context in one component and a store in another. Then decide which method is better for sharing the theme across multiple components.
📋 What You'll Learn
Create a Svelte component that sets a theme value using
setContextCreate a Svelte component that creates a writable
store for themeAccess the theme value using
getContext in a child componentSubscribe to the theme
store in another componentAdd a button to toggle the theme between 'light' and 'dark'
Explain which method is better for global theme sharing
💡 Why This Matters
🌍 Real World
Sharing theme or user preferences across many components in a Svelte app is common. Choosing between context and stores affects app design and reactivity.
💼 Career
Understanding when to use context vs stores helps you build scalable Svelte apps and collaborate with teams on state management.
Progress0 / 4 steps