Skip to content

Commit 92f9c69

Browse files
committed
Fix useContext documentation to correct context provider references
1 parent e9d2178 commit 92f9c69

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

src/content/reference/react/useContext.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ function MyComponent() {
3838
3939
#### Returns {/*returns*/}
4040
41-
`useContext` returns the context value for the calling component. It is determined as the `value` passed to the closest `SomeContext.Provider` above the calling component in the tree. If there is no such provider, then the returned value will be the `defaultValue` you have passed to [`createContext`](/reference/react/createContext) for that context. The returned value is always up-to-date. React automatically re-renders components that read some context if it changes.
41+
`useContext` returns the context value for the calling component. It is determined as the `value` passed to the closest `SomeContext` above the calling component in the tree. If there is no such provider, then the returned value will be the `defaultValue` you have passed to [`createContext`](/reference/react/createContext) for that context. The returned value is always up-to-date. React automatically re-renders components that read some context if it changes.
4242
4343
#### Caveats {/*caveats*/}
4444
45-
* `useContext()` call in a component is not affected by providers returned from the *same* component. The corresponding `<Context.Provider>` **needs to be *above*** the component doing the `useContext()` call.
45+
* `useContext()` call in a component is not affected by providers returned from the *same* component. The corresponding `<Context>` **needs to be *above*** the component doing the `useContext()` call.
4646
* React **automatically re-renders** all the children that use a particular context starting from the provider that receives a different `value`. The previous and the next values are compared with the [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison. Skipping re-renders with [`memo`](/reference/react/memo) does not prevent the children receiving fresh context values.
4747
* If your build system produces duplicates modules in the output (which can happen with symlinks), this can break context. Passing something via context only works if `SomeContext` that you use to provide context and `SomeContext` that you use to read it are ***exactly* the same object**, as determined by a `===` comparison.
4848
@@ -70,9 +70,9 @@ To pass context to a `Button`, wrap it or one of its parent components into the
7070
```js [[1, 3, "ThemeContext"], [2, 3, "\\"dark\\""], [1, 5, "ThemeContext"]]
7171
function MyPage() {
7272
return (
73-
<ThemeContext.Provider value="dark">
73+
<ThemeContext value="dark">
7474
<Form />
75-
</ThemeContext.Provider>
75+
</ThemeContext>
7676
);
7777
}
7878

@@ -98,9 +98,9 @@ const ThemeContext = createContext(null);
9898

9999
export default function MyApp() {
100100
return (
101-
<ThemeContext.Provider value="dark">
101+
<ThemeContext value="dark">
102102
<Form />
103-
</ThemeContext.Provider>
103+
</ThemeContext>
104104
)
105105
}
106106

@@ -183,14 +183,14 @@ Often, you'll want the context to change over time. To update context, combine i
183183
function MyPage() {
184184
const [theme, setTheme] = useState('dark');
185185
return (
186-
<ThemeContext.Provider value={theme}>
186+
<ThemeContext value={theme}>
187187
<Form />
188188
<Button onClick={() => {
189189
setTheme('light');
190190
}}>
191191
Switch to light theme
192192
</Button>
193-
</ThemeContext.Provider>
193+
</ThemeContext>
194194
);
195195
}
196196
```
@@ -213,7 +213,7 @@ const ThemeContext = createContext(null);
213213
export default function MyApp() {
214214
const [theme, setTheme] = useState('light');
215215
return (
216-
<ThemeContext.Provider value={theme}>
216+
<ThemeContext value={theme}>
217217
<Form />
218218
<label>
219219
<input
@@ -225,7 +225,7 @@ export default function MyApp() {
225225
/>
226226
Use dark mode
227227
</label>
228-
</ThemeContext.Provider>
228+
</ThemeContext>
229229
)
230230
}
231231

@@ -317,14 +317,14 @@ const CurrentUserContext = createContext(null);
317317
export default function MyApp() {
318318
const [currentUser, setCurrentUser] = useState(null);
319319
return (
320-
<CurrentUserContext.Provider
320+
<CurrentUserContext
321321
value={{
322322
currentUser,
323323
setCurrentUser
324324
}}
325325
>
326326
<Form />
327-
</CurrentUserContext.Provider>
327+
</CurrentUserContext>
328328
);
329329
}
330330

@@ -411,8 +411,8 @@ export default function MyApp() {
411411
const [theme, setTheme] = useState('light');
412412
const [currentUser, setCurrentUser] = useState(null);
413413
return (
414-
<ThemeContext.Provider value={theme}>
415-
<CurrentUserContext.Provider
414+
<ThemeContext value={theme}>
415+
<CurrentUserContext
416416
value={{
417417
currentUser,
418418
setCurrentUser
@@ -429,8 +429,8 @@ export default function MyApp() {
429429
/>
430430
Use dark mode
431431
</label>
432-
</CurrentUserContext.Provider>
433-
</ThemeContext.Provider>
432+
</CurrentUserContext>
433+
</ThemeContext>
434434
)
435435
}
436436

@@ -596,16 +596,16 @@ export default function MyApp() {
596596
function MyProviders({ children, theme, setTheme }) {
597597
const [currentUser, setCurrentUser] = useState(null);
598598
return (
599-
<ThemeContext.Provider value={theme}>
600-
<CurrentUserContext.Provider
599+
<ThemeContext value={theme}>
600+
<CurrentUserContext
601601
value={{
602602
currentUser,
603603
setCurrentUser
604604
}}
605605
>
606606
{children}
607-
</CurrentUserContext.Provider>
608-
</ThemeContext.Provider>
607+
</CurrentUserContext>
608+
</ThemeContext>
609609
);
610610
}
611611

@@ -775,11 +775,11 @@ export function TasksProvider({ children }) {
775775
);
776776

777777
return (
778-
<TasksContext.Provider value={tasks}>
779-
<TasksDispatchContext.Provider value={dispatch}>
778+
<TasksContext value={tasks}>
779+
<TasksDispatchContext value={dispatch}>
780780
{children}
781-
</TasksDispatchContext.Provider>
782-
</TasksContext.Provider>
781+
</TasksDispatchContext>
782+
</TasksContext>
783783
);
784784
}
785785

@@ -978,9 +978,9 @@ export default function MyApp() {
978978
const [theme, setTheme] = useState('light');
979979
return (
980980
<>
981-
<ThemeContext.Provider value={theme}>
981+
<ThemeContext value={theme}>
982982
<Form />
983-
</ThemeContext.Provider>
983+
</ThemeContext>
984984
<Button onClick={() => {
985985
setTheme(theme === 'dark' ? 'light' : 'dark');
986986
}}>
@@ -1067,13 +1067,13 @@ function Button({ children, onClick }) {
10671067
You can override the context for a part of the tree by wrapping that part in a provider with a different value.
10681068
10691069
```js {3,5}
1070-
<ThemeContext.Provider value="dark">
1070+
<ThemeContext value="dark">
10711071
...
1072-
<ThemeContext.Provider value="light">
1072+
<ThemeContext value="light">
10731073
<Footer />
1074-
</ThemeContext.Provider>
1074+
</ThemeContext>
10751075
...
1076-
</ThemeContext.Provider>
1076+
</ThemeContext>
10771077
```
10781078
10791079
You can nest and override providers as many times as you need.
@@ -1093,9 +1093,9 @@ const ThemeContext = createContext(null);
10931093

10941094
export default function MyApp() {
10951095
return (
1096-
<ThemeContext.Provider value="dark">
1096+
<ThemeContext value="dark">
10971097
<Form />
1098-
</ThemeContext.Provider>
1098+
</ThemeContext>
10991099
)
11001100
}
11011101

@@ -1104,9 +1104,9 @@ function Form() {
11041104
<Panel title="Welcome">
11051105
<Button>Sign up</Button>
11061106
<Button>Log in</Button>
1107-
<ThemeContext.Provider value="light">
1107+
<ThemeContext value="light">
11081108
<Footer />
1109-
</ThemeContext.Provider>
1109+
</ThemeContext>
11101110
</Panel>
11111111
);
11121112
}
@@ -1230,9 +1230,9 @@ export default function Section({ children }) {
12301230
const level = useContext(LevelContext);
12311231
return (
12321232
<section className="section">
1233-
<LevelContext.Provider value={level + 1}>
1233+
<LevelContext value={level + 1}>
12341234
{children}
1235-
</LevelContext.Provider>
1235+
</LevelContext>
12361236
</section>
12371237
);
12381238
}
@@ -1302,9 +1302,9 @@ function MyApp() {
13021302
}
13031303

13041304
return (
1305-
<AuthContext.Provider value={{ currentUser, login }}>
1305+
<AuthContext value={{ currentUser, login }}>
13061306
<Page />
1307-
</AuthContext.Provider>
1307+
</AuthContext>
13081308
);
13091309
}
13101310
```
@@ -1330,9 +1330,9 @@ function MyApp() {
13301330
}), [currentUser, login]);
13311331

13321332
return (
1333-
<AuthContext.Provider value={contextValue}>
1333+
<AuthContext value={contextValue}>
13341334
<Page />
1335-
</AuthContext.Provider>
1335+
</AuthContext>
13361336
);
13371337
}
13381338
```
@@ -1349,8 +1349,8 @@ Read more about [`useMemo`](/reference/react/useMemo#skipping-re-rendering-of-co
13491349
13501350
There are a few common ways that this can happen:
13511351
1352-
1. You're rendering `<SomeContext.Provider>` in the same component (or below) as where you're calling `useContext()`. Move `<SomeContext.Provider>` *above and outside* the component calling `useContext()`.
1353-
2. You may have forgotten to wrap your component with `<SomeContext.Provider>`, or you might have put it in a different part of the tree than you thought. Check whether the hierarchy is right using [React DevTools.](/learn/react-developer-tools)
1352+
1. You're rendering `<SomeContext>` in the same component (or below) as where you're calling `useContext()`. Move `<SomeContext>` *above and outside* the component calling `useContext()`.
1353+
2. You may have forgotten to wrap your component with `<SomeContext>`, or you might have put it in a different part of the tree than you thought. Check whether the hierarchy is right using [React DevTools.](/learn/react-developer-tools)
13541354
3. You might be running into some build issue with your tooling that causes `SomeContext` as seen from the providing component and `SomeContext` as seen by the reading component to be two different objects. This can happen if you use symlinks, for example. You can verify this by assigning them to globals like `window.SomeContext1` and `window.SomeContext2` and then checking whether `window.SomeContext1 === window.SomeContext2` in the console. If they're not the same, fix that issue on the build tool level.
13551355
13561356
### I am always getting `undefined` from my context although the default value is different {/*i-am-always-getting-undefined-from-my-context-although-the-default-value-is-different*/}
@@ -1359,9 +1359,9 @@ You might have a provider without a `value` in the tree:
13591359
13601360
```js {1,2}
13611361
// 🚩 Doesn't work: no value prop
1362-
<ThemeContext.Provider>
1362+
<ThemeContext>
13631363
<Button />
1364-
</ThemeContext.Provider>
1364+
</ThemeContext>
13651365
```
13661366
13671367
If you forget to specify `value`, it's like passing `value={undefined}`.
@@ -1370,18 +1370,18 @@ You may have also mistakingly used a different prop name by mistake:
13701370
13711371
```js {1,2}
13721372
// 🚩 Doesn't work: prop should be called "value"
1373-
<ThemeContext.Provider theme={theme}>
1373+
<ThemeContext theme={theme}>
13741374
<Button />
1375-
</ThemeContext.Provider>
1375+
</ThemeContext>
13761376
```
13771377
13781378
In both of these cases you should see a warning from React in the console. To fix them, call the prop `value`:
13791379
13801380
```js {1,2}
13811381
// ✅ Passing the value prop
1382-
<ThemeContext.Provider value={theme}>
1382+
<ThemeContext value={theme}>
13831383
<Button />
1384-
</ThemeContext.Provider>
1384+
</ThemeContext>
13851385
```
13861386
1387-
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.
1387+
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 value={undefined}>` component somewhere in the parent tree, the component calling `useContext(SomeContext)` *will* receive `undefined` as the context value.

0 commit comments

Comments
 (0)