Skip to content

Commit 599b55c

Browse files
committed
update
1 parent d992ace commit 599b55c

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/routes/reference/component-apis/create-context.mdx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ description: >-
1616
---
1717

1818
`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.
2020

2121
## Import
2222

@@ -37,6 +37,11 @@ function createContext<T>(
3737
defaultValue?: undefined,
3838
options?: { name?: string }
3939
): Context<T | undefined>;
40+
41+
function createContext<T>(
42+
defaultValue: T,
43+
options?: { name?: string }
44+
): Context<T>;
4045
```
4146

4247
## Parameters
@@ -64,9 +69,8 @@ Returns a context object containing `id`, `Provider`, and `defaultValue`.
6469
## Behavior
6570

6671
- `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.
7074

7175
## Notes
7276

@@ -91,11 +95,11 @@ const ThemeContext = createContext<Theme>("light");
9195
```tsx
9296
import { createContext } from "solid-js";
9397

94-
const CounterContext = createContext<number>();
98+
const CounterContext = createContext<{ count: number }>();
9599

96100
function CounterProvider(props) {
97101
return (
98-
<CounterContext.Provider value={1}>
102+
<CounterContext.Provider value={{ count: 1 }}>
99103
{props.children}
100104
</CounterContext.Provider>
101105
);

src/routes/reference/component-apis/use-context.mdx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ description: >-
1414
Read the current value of a context object created by `createContext`.
1515
---
1616

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.
1818

1919
## Import
2020

@@ -48,14 +48,14 @@ Context object created by [`createContext`](/reference/component-apis/create-con
4848
- **Type:** `T`
4949

5050
Returns the value provided by the nearest matching `Context.Provider`.
51+
If the context was created without a default value, `T` can include `undefined`.
5152
If no provider is found, it returns the context's default value or `undefined`.
5253

5354
## Behavior
5455

5556
- `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`.
5959

6060
## Examples
6161

@@ -75,6 +75,8 @@ function CounterValue() {
7575

7676
### Throw when a provider is missing
7777

78+
This example checks for `undefined` when the context was created without a default value.
79+
7880
```ts
7981
import { createContext, useContext } from "solid-js";
8082

0 commit comments

Comments
 (0)