Context API memungkinkan data dibagikan antar komponen tanpa prop drilling.
setContext
<!-- Parent.svelte -->
<script>
import { setContext } from "svelte"
import Child from "./Child.svelte"
setContext("theme", { color: "blue", size: "large" })
</script>
<Child />
getContext
<!-- Child.svelte (atau nested lebih dalam) -->
<script>
import { getContext } from "svelte"
const theme = getContext("theme")
</script>
<p style="color: {theme.color}">Teks bertema</p>
Perbedaan dengan Store
- Context — data tersedia untuk child tree saja, bisa berbeda per instance
- Store — global, satu nilai untuk semua komponen
Context cocok untuk theming, konfigurasi, atau data yang berbeda per subtree.