Skip to content

Commit 5a559e7

Browse files
committed
Capitalize Context in useContext.md
1 parent 27a8e45 commit 5a559e7

1 file changed

Lines changed: 34 additions & 34 deletions

File tree

‎src/content/reference/react/useContext.md‎

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: useContext
44

55
<Intro>
66

7-
`useContext` is a React Hook that lets you read and subscribe to [context](/learn/passing-data-deeply-with-context) from your component.
7+
`useContext` is a React Hook that lets you read and subscribe to [Context](/learn/passing-data-deeply-with-context) from your component.
88

99
```js
1010
const value = useContext(SomeContext)
@@ -20,7 +20,7 @@ const value = useContext(SomeContext)
2020

2121
### `useContext(SomeContext)` {/*usecontext*/}
2222

23-
Call `useContext` at the top level of your component to read and subscribe to [context.](/learn/passing-data-deeply-with-context)
23+
Call `useContext` at the top level of your component to read and subscribe to [Context.](/learn/passing-data-deeply-with-context)
2424

2525
```js
2626
import { useContext } from 'react';
@@ -34,17 +34,17 @@ function MyComponent() {
3434
3535
#### Parameters {/*parameters*/}
3636
37-
* `SomeContext`: The context that you've previously created with [`createContext`](/reference/react/createContext). The context itself does not hold the information, it only represents the kind of information you can provide or read from components.
37+
* `SomeContext`: The Context that you've previously created with [`createContext`](/reference/react/createContext). The Context itself does not hold the information, it only represents the kind of information you can provide or read from components.
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` 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
4545
* `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.
46-
* 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.
47-
* 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.
46+
* 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.
47+
* 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
4949
---
5050
@@ -53,7 +53,7 @@ function MyComponent() {
5353
5454
### Passing data deeply into the tree {/*passing-data-deeply-into-the-tree*/}
5555
56-
Call `useContext` at the top level of your component to read and subscribe to [context.](/learn/passing-data-deeply-with-context)
56+
Call `useContext` at the top level of your component to read and subscribe to [Context.](/learn/passing-data-deeply-with-context)
5757
5858
```js [[2, 4, "theme"], [1, 4, "ThemeContext"]]
5959
import { useContext } from 'react';
@@ -63,9 +63,9 @@ function Button() {
6363
// ...
6464
```
6565
66-
`useContext` returns the <CodeStep step={2}>context value</CodeStep> for the <CodeStep step={1}>context</CodeStep> you passed. To determine the context value, React searches the component tree and finds **the closest context provider above** for that particular context.
66+
`useContext` returns the <CodeStep step={2}>Context value</CodeStep> for the <CodeStep step={1}>Context</CodeStep> you passed. To determine the Context value, React searches the component tree and finds **the closest Context provider above** for that particular Context.
6767
68-
To pass context to a `Button`, wrap it or one of its parent components into the corresponding context provider:
68+
To pass Context to a `Button`, wrap it or one of its parent components into the corresponding Context provider:
6969
7070
```js [[1, 3, "ThemeContext"], [2, 3, "\\"dark\\""], [1, 5, "ThemeContext"]]
7171
function MyPage() {
@@ -175,9 +175,9 @@ function Button({ children }) {
175175
176176
---
177177
178-
### Updating data passed via context {/*updating-data-passed-via-context*/}
178+
### Updating data passed via Context {/*updating-data-passed-via-context*/}
179179
180-
Often, you'll want the context to change over time. To update context, combine it with [state.](/reference/react/useState) Declare a state variable in the parent component, and pass the current state down as the <CodeStep step={2}>context value</CodeStep> to the provider.
180+
Often, you'll want the Context to change over time. To update Context, combine it with [state.](/reference/react/useState) Declare a state variable in the parent component, and pass the current state down as the <CodeStep step={2}>Context value</CodeStep> to the provider.
181181
182182
```js {2} [[1, 4, "ThemeContext"], [2, 4, "theme"], [1, 11, "ThemeContext"]]
183183
function MyPage() {
@@ -197,11 +197,11 @@ function MyPage() {
197197
198198
Now any `Button` inside of the provider will receive the current `theme` value. If you call `setTheme` to update the `theme` value that you pass to the provider, all `Button` components will re-render with the new `'light'` value.
199199
200-
<Recipes titleText="Examples of updating context" titleId="examples-basic">
200+
<Recipes titleText="Examples of updating Context" titleId="examples-basic">
201201
202-
#### Updating a value via context {/*updating-a-value-via-context*/}
202+
#### Updating a value via Context {/*updating-a-value-via-context*/}
203203
204-
In this example, the `MyApp` component holds a state variable which is then passed to the `ThemeContext` provider. Checking the "Dark mode" checkbox updates the state. Changing the provided value re-renders all the components using that context.
204+
In this example, the `MyApp` component holds a state variable which is then passed to the `ThemeContext` provider. Checking the "Dark mode" checkbox updates the state. Changing the provided value re-renders all the components using that Context.
205205
206206
<Sandpack>
207207
@@ -299,13 +299,13 @@ function Button({ children }) {
299299
300300
</Sandpack>
301301
302-
Note that `value="dark"` passes the `"dark"` string, but `value={theme}` passes the value of the JavaScript `theme` variable with [JSX curly braces.](/learn/javascript-in-jsx-with-curly-braces) Curly braces also let you pass context values that aren't strings.
302+
Note that `value="dark"` passes the `"dark"` string, but `value={theme}` passes the value of the JavaScript `theme` variable with [JSX curly braces.](/learn/javascript-in-jsx-with-curly-braces) Curly braces also let you pass Context values that aren't strings.
303303
304304
<Solution />
305305
306-
#### Updating an object via context {/*updating-an-object-via-context*/}
306+
#### Updating an object via Context {/*updating-an-object-via-context*/}
307307
308-
In this example, there is a `currentUser` state variable which holds an object. You combine `{ currentUser, setCurrentUser }` into a single object and pass it down through the context inside the `value={}`. This lets any component below, such as `LoginButton`, read both `currentUser` and `setCurrentUser`, and then call `setCurrentUser` when needed.
308+
In this example, there is a `currentUser` state variable which holds an object. You combine `{ currentUser, setCurrentUser }` into a single object and pass it down through the Context inside the `value={}`. This lets any component below, such as `LoginButton`, read both `currentUser` and `setCurrentUser`, and then call `setCurrentUser` when needed.
309309
310310
<Sandpack>
311311
@@ -395,9 +395,9 @@ label {
395395
396396
<Solution />
397397
398-
#### Multiple contexts {/*multiple-contexts*/}
398+
#### Multiple Contexts {/*multiple-contexts*/}
399399
400-
In this example, there are two independent contexts. `ThemeContext` provides the current theme, which is a string, while `CurrentUserContext` holds the object representing the current user.
400+
In this example, there are two independent Contexts. `ThemeContext` provides the current theme, which is a string, while `CurrentUserContext` holds the object representing the current user.
401401
402402
<Sandpack>
403403
@@ -564,7 +564,7 @@ label {
564564
565565
#### Extracting providers to a component {/*extracting-providers-to-a-component*/}
566566
567-
As your app grows, it is expected that you'll have a "pyramid" of contexts closer to the root of your app. There is nothing wrong with that. However, if you dislike the nesting aesthetically, you can extract the providers into a single component. In this example, `MyProviders` hides the "plumbing" and renders the children passed to it inside the necessary providers. Note that the `theme` and `setTheme` state is needed in `MyApp` itself, so `MyApp` still owns that piece of the state.
567+
As your app grows, it is expected that you'll have a "pyramid" of Contexts closer to the root of your app. There is nothing wrong with that. However, if you dislike the nesting aesthetically, you can extract the providers into a single component. In this example, `MyProviders` hides the "plumbing" and renders the children passed to it inside the necessary providers. Note that the `theme` and `setTheme` state is needed in `MyApp` itself, so `MyApp` still owns that piece of the state.
568568
569569
<Sandpack>
570570
@@ -737,9 +737,9 @@ label {
737737
738738
<Solution />
739739
740-
#### Scaling up with context and a reducer {/*scaling-up-with-context-and-a-reducer*/}
740+
#### Scaling up with Context and a reducer {/*scaling-up-with-context-and-a-reducer*/}
741741
742-
In larger apps, it is common to combine context with a [reducer](/reference/react/useReducer) to extract the logic related to some state out of components. In this example, all the "wiring" is hidden in the `TasksContext.js`, which contains a reducer and two separate contexts.
742+
In larger apps, it is common to combine Context with a [reducer](/reference/react/useReducer) to extract the logic related to some state out of components. In this example, all the "wiring" is hidden in the `TasksContext.js`, which contains a reducer and two separate Contexts.
743743
744744
Read a [full walkthrough](/learn/scaling-up-with-reducer-and-context) of this example.
745745
@@ -949,13 +949,13 @@ ul, li { margin: 0; padding: 0; }
949949
950950
### Specifying a fallback default value {/*specifying-a-fallback-default-value*/}
951951
952-
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):
952+
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
954954
```js [[1, 1, "ThemeContext"], [3, 1, "null"]]
955955
const ThemeContext = createContext(null);
956956
```
957957
958-
The default value **never changes**. If you want to update context, use it with state as [described above.](#updating-data-passed-via-context)
958+
The default value **never changes**. If you want to update Context, use it with state as [described above.](#updating-data-passed-via-context)
959959
960960
Often, instead of `null`, there is some more meaningful value you can use as a default, for example:
961961
@@ -965,7 +965,7 @@ const ThemeContext = createContext('light');
965965
966966
This way, if you accidentally render some component without a corresponding provider, it won't break. This also helps your components work well in a test environment without setting up a lot of providers in the tests.
967967
968-
In the example below, the "Toggle theme" button is always light because it's **outside any theme context provider** and the default context theme value is `'light'`. Try editing the default theme to be `'dark'`.
968+
In the example below, the "Toggle theme" button is always light because it's **outside any theme Context provider** and the default Context theme value is `'light'`. Try editing the default theme to be `'dark'`.
969969
970970
<Sandpack>
971971
@@ -1062,9 +1062,9 @@ function Button({ children, onClick }) {
10621062
10631063
---
10641064
1065-
### Overriding context for a part of the tree {/*overriding-context-for-a-part-of-the-tree*/}
1065+
### Overriding Context for a part of the tree {/*overriding-context-for-a-part-of-the-tree*/}
10661066
1067-
You can override the context for a part of the tree by wrapping that part in a provider with a different value.
1067+
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}
10701070
<ThemeContext value="dark">
@@ -1078,11 +1078,11 @@ You can override the context for a part of the tree by wrapping that part in a p
10781078
10791079
You can nest and override providers as many times as you need.
10801080
1081-
<Recipes titleText="Examples of overriding context">
1081+
<Recipes titleText="Examples of overriding Context">
10821082
10831083
#### Overriding a theme {/*overriding-a-theme*/}
10841084
1085-
Here, the button *inside* the `Footer` receives a different context value (`"light"`) than the buttons outside (`"dark"`).
1085+
Here, the button *inside* the `Footer` receives a different Context value (`"light"`) than the buttons outside (`"dark"`).
10861086
10871087
<Sandpack>
10881088
@@ -1188,7 +1188,7 @@ footer {
11881188
11891189
#### Automatically nested headings {/*automatically-nested-headings*/}
11901190
1191-
You can "accumulate" information when you nest context providers. In this example, the `Section` component keeps track of the `LevelContext` which specifies the depth of the section nesting. It reads the `LevelContext` from the parent section, and provides the `LevelContext` number increased by one to its children. As a result, the `Heading` component can automatically decide which of the `<h1>`, `<h2>`, `<h3>`, ..., tags to use based on how many `Section` components it is nested inside of.
1191+
You can "accumulate" information when you nest Context providers. In this example, the `Section` component keeps track of the `LevelContext` which specifies the depth of the section nesting. It reads the `LevelContext` from the parent section, and provides the `LevelContext` number increased by one to its children. As a result, the `Heading` component can automatically decide which of the `<h1>`, `<h2>`, `<h3>`, ..., tags to use based on how many `Section` components it is nested inside of.
11921192
11931193
Read a [detailed walkthrough](/learn/passing-data-deeply-with-context) of this example.
11941194
@@ -1290,7 +1290,7 @@ export const LevelContext = createContext(0);
12901290
12911291
### Optimizing re-renders when passing objects and functions {/*optimizing-re-renders-when-passing-objects-and-functions*/}
12921292
1293-
You can pass any values via context, including objects and functions.
1293+
You can pass any values via Context, including objects and functions.
12941294
12951295
```js [[2, 10, "{ currentUser, login }"]]
12961296
function MyApp() {
@@ -1309,7 +1309,7 @@ function MyApp() {
13091309
}
13101310
```
13111311
1312-
Here, the <CodeStep step={2}>context value</CodeStep> is a JavaScript object with two properties, one of which is a function. Whenever `MyApp` re-renders (for example, on a route update), this will be a *different* object pointing at a *different* function, so React will also have to re-render all components deep in the tree that call `useContext(AuthContext)`.
1312+
Here, the <CodeStep step={2}>Context value</CodeStep> is a JavaScript object with two properties, one of which is a function. Whenever `MyApp` re-renders (for example, on a route update), this will be a *different* object pointing at a *different* function, so React will also have to re-render all components deep in the tree that call `useContext(AuthContext)`.
13131313
13141314
In smaller apps, this is not a problem. However, there is no need to re-render them if the underlying data, like `currentUser`, has not changed. To help React take advantage of that fact, you may wrap the `login` function with [`useCallback`](/reference/react/useCallback) and wrap the object creation into [`useMemo`](/reference/react/useMemo). This is a performance optimization:
13151315
@@ -1353,7 +1353,7 @@ There are a few common ways that this can happen:
13531353
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
1356-
### 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*/}
1356+
### 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*/}
13571357
13581358
You might have a provider without a `value` in the tree:
13591359
@@ -1384,4 +1384,4 @@ In both of these cases you should see a warning from React in the console. To fi
13841384
</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 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)