You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/svelte.dev/content/docs/svelte/06-runtime/02-context.md
+96-28Lines changed: 96 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,66 @@ NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-doc
3
3
title: Context
4
4
---
5
5
6
-
Context allows components to access values owned by parent components without passing them down as props (potentially through many layers of intermediate components, known as 'prop-drilling'). The parent component sets context with `setContext(key, value)`...
6
+
Context allows components to access values owned by parent components without passing them down as props (potentially through many layers of intermediate components, known as 'prop-drilling').
7
+
8
+
By creating a `[get, set]` pair of functions with `createContext`, you can set the context in a parent component and get it in a child component:
> [!NOTE]`createContext` was added in version 5.40. If you are using an earlier version of Svelte, you must use `setContext` and `getContext` instead.
60
+
61
+
This is particularly useful when `Parent.svelte` is not directly aware of `Child.svelte`, but instead renders it as part of a `children`[snippet](snippet) as shown above.
62
+
63
+
## `setContext` and `getContext`
64
+
65
+
As an alternative to `createContext`, you can use `setContext` and `getContext` directly. The parent component sets context with `setContext(key, value)`...
7
66
8
67
```svelte
9
68
<!--- file: Parent.svelte --->
@@ -27,32 +86,28 @@ Context allows components to access values owned by parent components without pa
27
86
<h1>{message}, inside Child.svelte</h1>
28
87
```
29
88
30
-
This is particularly useful when `Parent.svelte` is not directly aware of `Child.svelte`, but instead renders it as part of a `children`[snippet](snippet) ([demo](/playground/untitled#H4sIAAAAAAAAE42Q3W6DMAyFX8WyJgESK-oto6hTX2D3YxcM3IIUQpR40yqUd58CrCXsp7tL7HNsf2dAWXaEKR56yfTBGOOxFWQwfR6Qz8q1XAHjL-GjUhvzToJd7bU09FO9ctMkG0wxM5VuFeeFLLjtVK8ZnkpNkuGo-w6CTTJ9Z3PwsBAemlbUF934W8iy5DpaZtOUcU02-ZLcaS51jHEkTFm_kY1_wfOO8QnXrb8hBzDEc6pgZ4gFoyz4KgiD7nxfTe8ghqAhIfrJ46cTzVZBbkPlODVJsLCDO6V7ZcJoncyw1yRr0hd1GNn_ZbEM3I9i1bmVxOlWElUvDUNHxpQngt3C4CXzjS1rtvkw22wMrTRtTbC8Lkuabe7jvthPPe3DofYCAAA=)):
31
-
32
-
```svelte
33
-
<Parent>
34
-
<Child />
35
-
</Parent>
36
-
```
37
-
38
89
The key (`'my-context'`, in the example above) and the context itself can be any JavaScript value.
39
90
91
+
> [!NOTE]`createContext` is preferred since it provides better type safety and makes it unnecessary to use keys.
92
+
40
93
In addition to [`setContext`](svelte#setContext) and [`getContext`](svelte#getContext), Svelte exposes [`hasContext`](svelte#hasContext) and [`getAllContexts`](svelte#getAllContexts) functions.
41
94
42
95
## Using context with state
43
96
44
-
You can store reactive state in context ([demo](/playground/untitled#H4sIAAAAAAAAE41R0W6DMAz8FSuaBNUQdK8MkKZ-wh7HHihzu6hgosRMm1D-fUpSVNq12x4iEvvOx_kmQU2PIhfP3DCCJGgHYvxkkYid7NCI_GUS_KUcxhVEMjOelErNB3bsatvG4LW6n0ZsRC4K02qpuKqpZtmrQTNMYJA3QRAs7PTQQxS40eMCt3mX3duxnWb-lS5h7nTI0A4jMWoo4c44P_Hku-zrOazdy64chWo-ScfRkRgl8wgHKrLTH1OxHZkHgoHaTraHcopXUFYzPPVfuC_hwQaD1GrskdiNCdQwJljJqlvXfyqVsA5CGg0uRUQifHw56xFtciO75QrP07vo_JXf_tf8yK2ezDKY_ZWt_1y2qqYzv7bI1IW1V_sN19m-07wCAAA=))...
97
+
You can store reactive state in context...
45
98
99
+
<!-- codeblock:start {"title":"Context with state"} -->
46
100
```svelte
101
+
<!--- file: App.svelte --->
47
102
<script>
48
-
import { setContext } from 'svelte';
103
+
import { setCounter } from './context.ts';
49
104
import Child from './Child.svelte';
50
105
51
106
let counter = $state({
52
107
count: 0
53
108
});
54
109
55
-
setContext('counter', counter);
110
+
setCounter(counter);
56
111
</script>
57
112
58
113
<button onclick={() => counter.count += 1}>
@@ -62,12 +117,39 @@ You can store reactive state in context ([demo](/playground/untitled#H4sIAAAAAAA
...though note that if you _reassign_`counter` instead of updating it, you will 'break the link' — in other words instead of this...
68
150
69
151
```svelte
70
-
<button onclick={() => counter = { count: 0 }}>
152
+
<button onclick={() => counter = { count: 0 }}>
71
153
reset
72
154
</button>
73
155
```
@@ -82,21 +164,7 @@ You can store reactive state in context ([demo](/playground/untitled#H4sIAAAAAAA
82
164
83
165
Svelte will warn you if you get it wrong.
84
166
85
-
## Type-safe context
86
-
87
-
As an alternative to using `setContext` and `getContext` directly, you can use them via `createContext`. This gives you type safety and makes it unnecessary to use a key:
When writing [component tests](testing#Unit-and-component-tests-with-Vitest-Component-testing), it can be useful to create a wrapper component that sets the context in order to check the behaviour of a component that uses it. As of version 5.49, you can do this sort of thing:
0 commit comments