You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/scaling-up-with-reducer-and-context.md
+24-24Lines changed: 24 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -233,21 +233,21 @@ In a small example like this, this works well, but if you have tens or hundreds
233
233
234
234
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".**
235
235
236
-
Here is how you can combine a reducer with context:
236
+
Here is how you can combine a reducer with Context:
237
237
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.
241
241
242
-
### Step 1: Create the context {/*step-1-create-the-context*/}
242
+
### Step 1: Create the Context {/*step-1-create-the-context*/}
243
243
244
244
The `useReducer` Hook returns the current `tasks` and the `dispatch` function that lets you update them:
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.
452
452
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*/}
454
454
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:
### 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*/}
675
675
676
676
Now you don't need to pass the list of tasks or the event handlers down the tree:
677
677
@@ -693,7 +693,7 @@ export default function TaskList() {
693
693
// ...
694
694
```
695
695
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:
697
697
698
698
```js {3,9-13}
699
699
exportdefaultfunctionAddTask() {
@@ -713,7 +713,7 @@ export default function AddTask() {
713
713
// ...
714
714
```
715
715
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:
**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.
901
901
902
902
## Moving all wiring into a single file {/*moving-all-wiring-into-a-single-file*/}
903
903
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:
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`:
1125
1125
1126
1126
```js
1127
1127
exportfunctionuseTasks() {
@@ -1133,14 +1133,14 @@ export function useTasksDispatch() {
1133
1133
}
1134
1134
```
1135
1135
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:
1137
1137
1138
1138
```js
1139
1139
consttasks=useTasks();
1140
1140
constdispatch=useTasksDispatch();
1141
1141
```
1142
1142
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:**
1144
1144
1145
1145
<Sandpack>
1146
1146
@@ -1348,18 +1348,18 @@ Functions like `useTasks` and `useTasksDispatch` are called *[Custom Hooks.](/le
1348
1348
1349
1349
</Note>
1350
1350
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.
1352
1352
1353
1353
<Recap>
1354
1354
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.
1356
1356
- 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.
1360
1360
- 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.
1362
1362
- 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.
Copy file name to clipboardExpand all lines: src/content/learn/typescript.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -244,7 +244,7 @@ export default function App() {
244
244
245
245
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.
246
246
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:
248
248
249
249
<Sandpack>
250
250
@@ -286,7 +286,7 @@ export default App = AppTSX;
286
286
287
287
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`.
288
288
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:
Copy file name to clipboardExpand all lines: src/content/reference/react/PureComponent.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,7 +63,7 @@ class Greeting extends PureComponent {
63
63
}
64
64
```
65
65
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.
67
67
68
68
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):
0 commit comments