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: beta/src/pages/learn/extracting-state-logic-into-a-reducer.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ Components with many state updates spread across many event handlers can get ove
19
19
20
20
## Consolidate state logic with a reducer {/*consolidate-state-logic-with-a-reducer*/}
21
21
22
-
As your components grow in complexity, it can get harder to see all the different ways that a component's state gets updated at a glance. For example, the `TaskBoard` component below holds an array of `tasks` in state and uses three different event handlers to add, remove, and edit tasks:
22
+
As your components grow in complexity, it can get harder to see all the different ways that a component's state gets updated at a glance. For example, the `TaskApp` component below holds an array of `tasks` in state and uses three different event handlers to add, remove, and edit tasks:
23
23
24
24
<Sandpack>
25
25
@@ -28,7 +28,7 @@ import { useState } from 'react';
28
28
importAddTaskfrom'./AddTask.js';
29
29
importTaskListfrom'./TaskList.js';
30
30
31
-
exportdefaultfunctionTaskBoard() {
31
+
exportdefaultfunctionTaskApp() {
32
32
const [tasks, setTasks] =useState(initialTasks);
33
33
34
34
functionhandleAddTask(text) {
@@ -492,7 +492,7 @@ import { useReducer } from 'react';
492
492
importAddTaskfrom'./AddTask.js';
493
493
importTaskListfrom'./TaskList.js';
494
494
495
-
exportdefaultfunctionTaskBoard() {
495
+
exportdefaultfunctionTaskApp() {
496
496
const [tasks, dispatch] =useReducer(
497
497
tasksReducer,
498
498
initialTasks
@@ -682,7 +682,7 @@ import AddTask from './AddTask.js';
682
682
importTaskListfrom'./TaskList.js';
683
683
importtasksReducerfrom'./tasksReducer.js';
684
684
685
-
exportdefaultfunctionTaskBoard() {
685
+
exportdefaultfunctionTaskApp() {
686
686
const [tasks, dispatch] =useReducer(
687
687
tasksReducer,
688
688
initialTasks
@@ -925,7 +925,7 @@ function tasksReducer(draft, action) {
925
925
}
926
926
}
927
927
928
-
exportdefaultfunctionTaskBoard() {
928
+
exportdefaultfunctionTaskApp() {
929
929
const [tasks, dispatch] =useImmerReducer(
930
930
tasksReducer,
931
931
initialTasks
@@ -2804,4 +2804,4 @@ This is because the dispatched actions are queued until the next render, [simila
A reducer helps keep the event handlers short and concise. However, as your app grows, you might run into another difficulty. **Currently, the `tasks` state and the `dispatch` function are only available in the top-level `TaskBoard` component.** To let other components read the list of tasks or change it, you have to explicitly [pass down](/learn/passing-props-to-a-component) the current state and the event handlers that change it as props.
210
+
A reducer helps keep the event handlers short and concise. However, as your app grows, you might run into another difficulty. **Currently, the `tasks` state and the `dispatch` function are only available in the top-level `TaskApp` component.** To let other components read the list of tasks or change it, you have to explicitly [pass down](/learn/passing-props-to-a-component) the current state and the event handlers that change it as props.
211
211
212
-
For example, `TaskBoard` passes a list of tasks and the event handlers to `TaskList`:
212
+
For example, `TaskApp` passes a list of tasks and the event handlers to `TaskList`:
213
213
214
214
```js
215
215
<TaskList
@@ -233,7 +233,7 @@ In a small example like this, this works well, but if you have tens or hundreds
233
233
234
234
<!--(TODO: illustration of prop drilling)-->
235
235
236
-
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 `TaskBoard` in the tree can read the tasks and dispatch actions without the repetitive "prop drilling".**
236
+
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".**
237
237
238
238
<!--(TODO: illustration of context)-->
239
239
@@ -265,7 +265,7 @@ import { useReducer } from 'react';
Here, you're passing `null` as the default value to both contexts. The actual values will be provided by the `TaskBoard` component.
455
+
Here, you're passing `null` as the default value to both contexts. The actual values will be provided by the `TaskApp` component.
456
456
457
457
### Step 2: Put state and dispatch into context {/*step-2-put-state-and-dispatch-into-context*/}
458
458
459
-
Now you can import both contexts in your `TaskBoard` 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:
459
+
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:
**The `TaskBoard` 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:
720
+
**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:
721
721
722
722
<Sandpack>
723
723
@@ -727,7 +727,7 @@ import AddTask from './AddTask.js';
**The state still "lives" in the top-level `TaskBoard` 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.
904
+
**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.
905
905
906
906
## Moving all wiring into a single file {/*moving-all-wiring-into-a-single-file*/}
907
907
@@ -934,7 +934,7 @@ export function TasksProvider({ children }) {
934
934
}
935
935
```
936
936
937
-
**This removes all the complexity and wiring from your `TaskBoard` component:**
937
+
**This removes all the complexity and wiring from your `TaskApp` component:**
938
938
939
939
<Sandpack>
940
940
@@ -943,7 +943,7 @@ import AddTask from './AddTask.js';
943
943
importTaskListfrom'./TaskList.js';
944
944
import { TasksProvider } from'./TasksContext.js';
945
945
946
-
exportdefaultfunctionTaskBoard() {
946
+
exportdefaultfunctionTaskApp() {
947
947
return (
948
948
<TasksProvider>
949
949
<h1>Day off in Kyoto</h1>
@@ -1153,7 +1153,7 @@ import AddTask from './AddTask.js';
0 commit comments