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/reference/component-apis/create-context.mdx
+10-6Lines changed: 10 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ description: >-
16
16
---
17
17
18
18
`createContext` creates a context object.
19
-
The returned object contains a `Provider` component and a`defaultValue`; [`useContext`](/reference/component-apis/use-context) reads the nearest matching provider value.
19
+
The returned object contains a `Provider` component and an optional`defaultValue`; [`useContext`](/reference/component-apis/use-context) reads the nearest matching provider value.
20
20
21
21
## Import
22
22
@@ -37,6 +37,11 @@ function createContext<T>(
37
37
defaultValue?:undefined,
38
38
options?: { name?:string }
39
39
):Context<T|undefined>;
40
+
41
+
function createContext<T>(
42
+
defaultValue:T,
43
+
options?: { name?:string }
44
+
):Context<T>;
40
45
```
41
46
42
47
## Parameters
@@ -64,9 +69,8 @@ Returns a context object containing `id`, `Provider`, and `defaultValue`.
64
69
## Behavior
65
70
66
71
-`Context.Provider` passes its `value` prop to descendant calls to [`useContext`](/reference/component-apis/use-context).
67
-
- If no matching provider is found, `useContext` returns `defaultValue` when one was supplied.
68
-
- If no matching provider is found and no default value was supplied, `useContext` returns `undefined`.
69
-
- Calling `useContext` outside the owner tree of a matching provider also returns the default value or `undefined`.
72
+
-`useContext` reads the nearest matching provider in the current owner tree.
73
+
- If no matching provider is found, `useContext` returns `defaultValue` when one was supplied, or `undefined` otherwise.
Copy file name to clipboardExpand all lines: src/routes/reference/component-apis/use-context.mdx
+6-4Lines changed: 6 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ description: >-
14
14
Read the current value of a context object created by `createContext`.
15
15
---
16
16
17
-
`useContext`returns the current value for a context object.
17
+
`useContext`reads the nearest provider value for a context object in the current owner tree.
18
18
19
19
## Import
20
20
@@ -48,14 +48,14 @@ Context object created by [`createContext`](/reference/component-apis/create-con
48
48
-**Type:**`T`
49
49
50
50
Returns the value provided by the nearest matching `Context.Provider`.
51
+
If the context was created without a default value, `T` can include `undefined`.
51
52
If no provider is found, it returns the context's default value or `undefined`.
52
53
53
54
## Behavior
54
55
55
56
-`useContext` reads the nearest matching provider in the current owner tree.
56
-
- When no provider is found, it returns the default value from [`createContext`](/reference/component-apis/create-context).
57
-
- When no provider is found and no default value was supplied, the result is `undefined`.
58
-
- Calling `useContext` outside the owner tree of a matching provider returns the default value or `undefined`.
57
+
- If no matching provider is found, it returns the default value from [`createContext`](/reference/component-apis/create-context), or `undefined` when no default value was supplied.
58
+
- A provider value of `undefined` is treated the same as a missing provider and returns the default value or `undefined`.
59
59
60
60
## Examples
61
61
@@ -75,6 +75,8 @@ function CounterValue() {
75
75
76
76
### Throw when a provider is missing
77
77
78
+
This example checks for `undefined` when the context was created without a default value.
0 commit comments