Skip to content

Commit 07bcb16

Browse files
committed
docs: Add note about using use hook in useContext documentation
1 parent e148ffe commit 07bcb16

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/content/reference/react/useContext.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,14 @@ ul, li { margin: 0; padding: 0; }
951951
952952
If React can't find any providers of that particular <CodeStep step={1}>context</CodeStep> in the parent tree, the context value returned by `useContext()` will be equal to the <CodeStep step={3}>default value</CodeStep> that you specified when you [created that context](/reference/react/createContext):
953953
954+
955+
956+
957+
958+
959+
960+
961+
954962
```js [[1, 1, "ThemeContext"], [3, 1, "null"]]
955963
const ThemeContext = createContext(null);
956964
```
@@ -1385,3 +1393,18 @@ In both of these cases you should see a warning from React in the console. To fi
13851393
```
13861394
13871395
Note that the [default value from your `createContext(defaultValue)` call](#specifying-a-fallback-default-value) is only used **if there is no matching provider above at all.** If there is a `<SomeContext.Provider value={undefined}>` component somewhere in the parent tree, the component calling `useContext(SomeContext)` *will* receive `undefined` as the context value.
1396+
1397+
**Note:** In Server Components (e.g., in Next.js), you can also use the [`use`](https://react.dev/reference/react/use) hook to read from context. It provides more flexibility, supports promises, and works in Server Components—making it more suitable in many modern use cases than `useContext`.
1398+
1399+
🧩 Why Developers Should Be More Aware
1400+
✅ Works in Server Components
1401+
use() allows you to access context values inside Server Components, while useContext() is limited to Client Components.
1402+
1403+
🔮 Future-Proofing Your Code
1404+
Since React is shifting towards more Server Components (especially with Next.js), using use() makes your code more modern and compatible.
1405+
1406+
🧵 Handles Async Better
1407+
You can use() a promise directly. You can't do that with useContext().
1408+
1409+
📚 Consistency in Docs
1410+
The current page on useContext doesn't mention that use() can be used instead in many cases. Developers may miss this important update if it’s not noted.

0 commit comments

Comments
 (0)