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
{{ message }}
Repository navigation
Commit 2f684fe
Browse filesBrowse the repository at this point in the historyBrowse files
docs: clarify that context lookup includes the current component and all ancestors (#18581)
Fixes#7916. Context lookup starts at the current component, not the
closest parent. `setContext` followed by `getContext` with the same key
in the same component returns the value.
---------
Co-authored-by: Simon H <[email protected]>
Copy file name to clipboardExpand all lines: documentation/docs/98-reference/.generated/shared-errors.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,10 +75,10 @@ Certain lifecycle methods can only be used during component initialisation. To f
75
75
### missing_context
76
76
77
77
```
78
-
Context was not set in a parent component
78
+
Context was not set in the current component or any of its ancestors
79
79
```
80
80
81
-
The [`createContext()`](svelte#createContext) utility returns a `[get, set]` pair of functions. `get` will throw an error if `set` was not used to set the context in a parent component.
81
+
The [`createContext()`](svelte#createContext) utility returns a `[get, set]` pair of functions. `get` will throw an error if `set` was not used to set the context in the current component or any of its ancestors.
Copy file name to clipboardExpand all lines: packages/svelte/messages/shared-errors/errors.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -62,9 +62,9 @@ Certain lifecycle methods can only be used during component initialisation. To f
62
62
63
63
## missing_context
64
64
65
-
> Context was not set in a parent component
65
+
> Context was not set in the current component or any of its ancestors
66
66
67
-
The [`createContext()`](svelte#createContext) utility returns a `[get, set]` pair of functions. `get` will throw an error if `set` was not used to set the context in a parent component.
67
+
The [`createContext()`](svelte#createContext) utility returns a `[get, set]` pair of functions. `get` will throw an error if `set` was not used to set the context in the current component or any of its ancestors.
Copy file name to clipboardExpand all lines: packages/svelte/src/internal/client/context.js
+12-9Lines changed: 12 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -73,7 +73,8 @@ export function set_dev_current_component_function(fn) {
73
73
/**
74
74
* Returns a `[get, set]` pair of functions for working with context in a type-safe way.
75
75
*
76
-
* `get` will throw an error if no parent component called `set`.
76
+
* `get` will throw an error if `set` has not yet been called in the current component or any of
77
+
* its ancestors.
77
78
*
78
79
* @template T
79
80
* @returns {[() => T, (context: T) => T]}
@@ -86,7 +87,9 @@ export function createContext() {
86
87
}
87
88
88
89
/**
89
-
* Retrieves the context that belongs to the closest parent component with the specified `key`.
90
+
* Retrieves the context set with the specified `key` in the current component or any of its
91
+
* ancestors. If multiple components set the same key, the value from the closest one is returned.
92
+
* A `setContext` call in the current component is only visible to `getContext` calls that run after it.
90
93
* Must be called during component initialisation.
91
94
*
92
95
* [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative.
@@ -103,8 +106,8 @@ export function getContext(key) {
103
106
104
107
/**
105
108
* Associates an arbitrary `context` object with the current component and the specified `key`
106
-
* and returns that object. The context is then available to children of the component
107
-
* (including slotted content) with `getContext`.
109
+
* and returns that object. The context is then available to the component itself and all of its
110
+
* descendants (including slotted content) with `getContext`.
108
111
*
109
112
* Like lifecycle functions, this must be called during component initialisation.
110
113
*
@@ -136,8 +139,8 @@ export function setContext(key, context) {
136
139
}
137
140
138
141
/**
139
-
* Checks whether a given `key` has been set in the context of a parent component.
140
-
* Must be called during component initialisation.
142
+
* Checks whether a given `key` has been set in the context of the current component or any of
143
+
* its ancestors. Must be called during component initialisation.
141
144
*
142
145
* @param {any} key
143
146
* @returns {boolean}
@@ -148,9 +151,9 @@ export function hasContext(key) {
148
151
}
149
152
150
153
/**
151
-
* Retrieves the whole context map that belongs to the closest parent component.
152
-
* Must be called during component initialisation. Useful, for example, if you
153
-
* programmatically create a component and want to pass the existing context to it.
154
+
* Retrieves the whole context map that belongs to the current component, including entries
155
+
* inherited from its ancestors. Must be called during component initialisation. Useful, for
156
+
* example, if you programmatically create a component and want to pass the existing context to it.
Copy file name to clipboardExpand all lines: packages/svelte/src/internal/shared/errors.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -86,12 +86,12 @@ export function lifecycle_outside_component(name) {
86
86
}
87
87
88
88
/**
89
-
* Context was not set in a parent component
89
+
* Context was not set in the current component or any of its ancestors
90
90
* @returns {never}
91
91
*/
92
92
exportfunctionmissing_context(){
93
93
if(DEV){
94
-
consterror=newError(`missing_context\nContext was not set in a parent component\nhttps://svelte.dev/e/missing_context`);
94
+
consterror=newError(`missing_context\nContext was not set in the current component or any of its ancestors\nhttps://svelte.dev/e/missing_context`);
* Retrieves the context that belongs to the closest parent component with the specified `key`.
504
+
* Retrieves the context set with the specified `key` in the current component or any of its
505
+
* ancestors. If multiple components set the same key, the value from the closest one is returned.
506
+
* A `setContext` call in the current component is only visible to `getContext` calls that run after it.
504
507
* Must be called during component initialisation.
505
508
*
506
509
* [`createContext`](https://svelte.dev/docs/svelte/svelte#createContext) is a type-safe alternative.
@@ -509,8 +512,8 @@ declare module 'svelte' {
509
512
exportfunctiongetContext<T>(key: any): T;
510
513
/**
511
514
* Associates an arbitrary `context` object with the current component and the specified `key`
512
-
* and returns that object. The context is then available to children of the component
513
-
* (including slotted content) with `getContext`.
515
+
* and returns that object. The context is then available to the component itself and all of its
516
+
* descendants (including slotted content) with `getContext`.
514
517
*
515
518
* Like lifecycle functions, this must be called during component initialisation.
0 commit comments