Skip to content

Commit a983b33

Browse files
committed
docs: capitalize Context as a React concept
1 parent 2639f36 commit a983b33

8 files changed

Lines changed: 132 additions & 134 deletions

File tree

src/content/learn/passing-data-deeply-with-context.md

Lines changed: 38 additions & 40 deletions
Large diffs are not rendered by default.

src/content/learn/scaling-up-with-reducer-and-context.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,21 @@ In a small example like this, this works well, but if you have tens or hundreds
233233

234234
This is why, as an alternative to passing them through props, you might want to put both the `tasks` state and the `dispatch` function [into context.](/learn/passing-data-deeply-with-context) **This way, any component below `TaskApp` in the tree can read the tasks and dispatch actions without the repetitive "prop drilling".**
235235

236-
Here is how you can combine a reducer with context:
236+
Here is how you can combine a reducer with Context:
237237

238-
1. **Create** the context.
239-
2. **Put** state and dispatch into context.
240-
3. **Use** context anywhere in the tree.
238+
1. **Create** the Context.
239+
2. **Put** state and dispatch into Context.
240+
3. **Use** Context anywhere in the tree.
241241

242-
### Step 1: Create the context {/*step-1-create-the-context*/}
242+
### Step 1: Create the Context {/*step-1-create-the-context*/}
243243

244244
The `useReducer` Hook returns the current `tasks` and the `dispatch` function that lets you update them:
245245

246246
```js
247247
const [tasks, dispatch] = useReducer(tasksReducer, initialTasks);
248248
```
249249

250-
To pass them down the tree, you will [create](/learn/passing-data-deeply-with-context#step-2-use-the-context) two separate contexts:
250+
To pass them down the tree, you will [create](/learn/passing-data-deeply-with-context#step-2-use-the-context) two separate Contexts:
251251

252252
- `TasksContext` provides the current list of tasks.
253253
- `TasksDispatchContext` provides the function that lets components dispatch actions.
@@ -448,11 +448,11 @@ ul, li { margin: 0; padding: 0; }
448448

449449
</Sandpack>
450450

451-
Here, you're passing `null` as the default value to both contexts. The actual values will be provided by the `TaskApp` component.
451+
Here, you're passing `null` as the default value to both Contexts. The actual values will be provided by the `TaskApp` component.
452452

453-
### Step 2: Put state and dispatch into context {/*step-2-put-state-and-dispatch-into-context*/}
453+
### Step 2: Put state and dispatch into Context {/*step-2-put-state-and-dispatch-into-context*/}
454454

455-
Now you can import both contexts in your `TaskApp` component. Take the `tasks` and `dispatch` returned by `useReducer()` and [provide them](/learn/passing-data-deeply-with-context#step-3-provide-the-context) to the entire tree below:
455+
Now you can import both Contexts in your `TaskApp` component. Take the `tasks` and `dispatch` returned by `useReducer()` and [provide them](/learn/passing-data-deeply-with-context#step-3-provide-the-context) to the entire tree below:
456456

457457
```js {4,7-8}
458458
import { TasksContext, TasksDispatchContext } from './TasksContext.js';
@@ -671,7 +671,7 @@ ul, li { margin: 0; padding: 0; }
671671

672672
In the next step, you will remove prop passing.
673673

674-
### Step 3: Use context anywhere in the tree {/*step-3-use-context-anywhere-in-the-tree*/}
674+
### Step 3: Use Context anywhere in the tree {/*step-3-use-context-anywhere-in-the-tree*/}
675675

676676
Now you don't need to pass the list of tasks or the event handlers down the tree:
677677

@@ -693,7 +693,7 @@ export default function TaskList() {
693693
// ...
694694
```
695695
696-
To update the task list, any component can read the `dispatch` function from context and call it:
696+
To update the task list, any component can read the `dispatch` function from Context and call it:
697697
698698
```js {3,9-13}
699699
export default function AddTask() {
@@ -713,7 +713,7 @@ export default function AddTask() {
713713
// ...
714714
```
715715
716-
**The `TaskApp` component does not pass any event handlers down, and the `TaskList` does not pass any event handlers to the `Task` component either.** Each component reads the context that it needs:
716+
**The `TaskApp` component does not pass any event handlers down, and the `TaskList` does not pass any event handlers to the `Task` component either.** Each component reads the Context that it needs:
717717
718718
<Sandpack>
719719
@@ -897,11 +897,11 @@ ul, li { margin: 0; padding: 0; }
897897
898898
</Sandpack>
899899
900-
**The state still "lives" in the top-level `TaskApp` component, managed with `useReducer`.** But its `tasks` and `dispatch` are now available to every component below in the tree by importing and using these contexts.
900+
**The state still "lives" in the top-level `TaskApp` component, managed with `useReducer`.** But its `tasks` and `dispatch` are now available to every component below in the tree by importing and using these Contexts.
901901
902902
## Moving all wiring into a single file {/*moving-all-wiring-into-a-single-file*/}
903903
904-
You don't have to do this, but you could further declutter the components by moving both reducer and context into a single file. Currently, `TasksContext.js` contains only two context declarations:
904+
You don't have to do this, but you could further declutter the components by moving both reducer and Context into a single file. Currently, `TasksContext.js` contains only two Context declarations:
905905
906906
```js
907907
import { createContext } from 'react';
@@ -1121,7 +1121,7 @@ ul, li { margin: 0; padding: 0; }
11211121
11221122
</Sandpack>
11231123
1124-
You can also export functions that _use_ the context from `TasksContext.js`:
1124+
You can also export functions that _use_ the Context from `TasksContext.js`:
11251125
11261126
```js
11271127
export function useTasks() {
@@ -1133,14 +1133,14 @@ export function useTasksDispatch() {
11331133
}
11341134
```
11351135
1136-
When a component needs to read context, it can do it through these functions:
1136+
When a component needs to read Context, it can do it through these functions:
11371137
11381138
```js
11391139
const tasks = useTasks();
11401140
const dispatch = useTasksDispatch();
11411141
```
11421142
1143-
This doesn't change the behavior in any way, but it lets you later split these contexts further or add some logic to these functions. **Now all of the context and reducer wiring is in `TasksContext.js`. This keeps the components clean and uncluttered, focused on what they display rather than where they get the data:**
1143+
This doesn't change the behavior in any way, but it lets you later split these Contexts further or add some logic to these functions. **Now all of the Context and reducer wiring is in `TasksContext.js`. This keeps the components clean and uncluttered, focused on what they display rather than where they get the data:**
11441144
11451145
<Sandpack>
11461146
@@ -1348,18 +1348,18 @@ Functions like `useTasks` and `useTasksDispatch` are called *[Custom Hooks.](/le
13481348
13491349
</Note>
13501350
1351-
As your app grows, you may have many context-reducer pairs like this. This is a powerful way to scale your app and [lift state up](/learn/sharing-state-between-components) without too much work whenever you want to access the data deep in the tree.
1351+
As your app grows, you may have many Context-reducer pairs like this. This is a powerful way to scale your app and [lift state up](/learn/sharing-state-between-components) without too much work whenever you want to access the data deep in the tree.
13521352
13531353
<Recap>
13541354
1355-
- You can combine reducer with context to let any component read and update state above it.
1355+
- You can combine reducer with Context to let any component read and update state above it.
13561356
- To provide state and the dispatch function to components below:
1357-
1. Create two contexts (for state and for dispatch functions).
1358-
2. Provide both contexts from the component that uses the reducer.
1359-
3. Use either context from components that need to read them.
1357+
1. Create two Contexts (for state and for dispatch functions).
1358+
2. Provide both Contexts from the component that uses the reducer.
1359+
3. Use either Context from components that need to read them.
13601360
- You can further declutter the components by moving all wiring into one file.
1361-
- You can export a component like `TasksProvider` that provides context.
1361+
- You can export a component like `TasksProvider` that provides Context.
13621362
- You can also export custom Hooks like `useTasks` and `useTasksDispatch` to read it.
1363-
- You can have many context-reducer pairs like this in your app.
1363+
- You can have many Context-reducer pairs like this in your app.
13641364
13651365
</Recap>

src/content/learn/typescript.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ export default function App() {
244244

245245
The [`useContext` Hook](/reference/react/useContext) is a technique for passing data down the component tree without having to pass props through components. It is used by creating a provider component and often by creating a Hook to consume the value in a child component.
246246

247-
The type of the value provided by the context is inferred from the value passed to the `createContext` call:
247+
The type of the value provided by the Context is inferred from the value passed to the `createContext` call:
248248

249249
<Sandpack>
250250

@@ -286,7 +286,7 @@ export default App = AppTSX;
286286

287287
This technique works when you have a default value which makes sense - but there are occasionally cases when you do not, and in those cases `null` can feel reasonable as a default value. However, to allow the type-system to understand your code, you need to explicitly set `ContextShape | null` on the `createContext`.
288288

289-
This causes the issue that you need to eliminate the `| null` in the type for context consumers. Our recommendation is to have the Hook do a runtime check for it's existence and throw an error when not present:
289+
This causes the issue that you need to eliminate the `| null` in the type for Context consumers. Our recommendation is to have the Hook do a runtime check for its existence and throw an error when not present:
290290

291291
```js {5, 16-20}
292292
import { createContext, useContext, useState, useMemo } from 'react';
@@ -296,7 +296,7 @@ type ComplexObject = {
296296
kind: string
297297
};
298298

299-
// The context is created with `| null` in the type, to accurately reflect the default value.
299+
// The Context is created with `| null` in the type, to accurately reflect the default value.
300300
const Context = createContext<ComplexObject | null>(null);
301301

302302
// The `| null` will be removed via the check in the Hook.

src/content/reference/react/PureComponent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class Greeting extends PureComponent {
6363
}
6464
```
6565

66-
A React component should always have [pure rendering logic.](/learn/keeping-components-pure) This means that it must return the same output if its props, state, and context haven't changed. By using `PureComponent`, you are telling React that your component complies with this requirement, so React doesn't need to re-render as long as its props and state haven't changed. However, your component will still re-render if a context that it's using changes.
66+
A React component should always have [pure rendering logic.](/learn/keeping-components-pure) This means that it must return the same output if its props, state, and Context haven't changed. By using `PureComponent`, you are telling React that your component complies with this requirement, so React doesn't need to re-render as long as its props and state haven't changed. However, your component will still re-render if a Context that it's using changes.
6767

6868
In this example, notice that the `Greeting` component re-renders whenever `name` is changed (because that's one of its props), but not when `address` is changed (because it's not passed to `Greeting` as a prop):
6969

0 commit comments

Comments
 (0)