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: src/routes/concepts/context.mdx
+13-24
Original file line number
Diff line number
Diff line change
@@ -21,16 +21,13 @@ This offers a way to access state across an application without passing props th
21
21
Context is created using the [`createContext`](/reference/component-apis/create-context) function.
22
22
This function has a `Provider` property that wraps the component tree you want to provide context to.
23
23
24
-
<TabsCodeBlocks>
25
-
<divid="/context/create.js">
26
-
```jsx
24
+
```jsx tab title="/context/create.js"
27
25
import { createContext } from"solid-js";
28
26
29
27
constMyContext=createContext();
30
28
```
31
-
</div>
32
-
<divid="/context/component.jsx">
33
-
```jsx
29
+
30
+
```jsx tab title="/context/component.jsx"
34
31
import { MyContext } from"./create";
35
32
36
33
exportfunctionProvider (props) {
@@ -41,8 +38,6 @@ export function Provider (props) {
41
38
)
42
39
};
43
40
```
44
-
</div>
45
-
</TabsCodeBlocks>
46
41
47
42
## Providing context to children
48
43
@@ -161,9 +156,7 @@ export function CounterProvider(props) {
161
156
[Signals](/concepts/signals) offer a way to synchronize and manage data shared across your components using context.
162
157
You can pass a signal directly to the `value` prop of the `Provider` component, and any changes to the signal will be reflected in all components that consume the context.
@@ -221,8 +213,6 @@ export function Child(props) {
221
213
);
222
214
};
223
215
```
224
-
</div>
225
-
</TabsCodeBlocks>
226
216
227
217
This offers a way to manage state across your components without having to pass props through intermediate elements.
228
218
@@ -262,12 +252,12 @@ Read more about default values in the [`createContext`](/reference/component-api
262
252
:::
263
253
264
254
Because of this, if an initial value was not passed to `createContext`, the TS type signature of `useContext` will indicate that
265
-
the value returned might be `undefined` (as mentioned above).
266
-
This can be quite annoying when you want to use the context inside a component, and particularly when immediately destructuring the context.
255
+
the value returned might be `undefined` (as mentioned above).
256
+
This can be quite annoying when you want to use the context inside a component, and particularly when immediately destructuring the context.
267
257
Additionally, if you use `useContext` and it returns `undefined` (which is often, but not always, the result of a bug), the error message thrown at runtime can be confusing.
268
258
269
-
The most common solution for it is to wrap all uses of `useContext` in a function that will explicitly throw a helpful error if the context is `undefined`.
270
-
This also serves to narrow the type returned, so TS doesn't complain.
259
+
The most common solution for it is to wrap all uses of `useContext` in a function that will explicitly throw a helpful error if the context is `undefined`.
260
+
This also serves to narrow the type returned, so TS doesn't complain.
271
261
As an example:
272
262
273
263
```ts title="/context/counter-component.tsx"
@@ -279,4 +269,3 @@ function useCounterContext() {
0 commit comments