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
+55-55
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
---
2
-
title: Scaling Up with Reducer and Context
2
+
title: リデューサとコンテクストでスケールアップ
3
3
---
4
4
5
5
<Intro>
6
6
7
-
Reducers let you consolidate a component's state update logic. Context lets you pass information deep down to other components. You can combine reducers and context together to manage state of a complex screen.
7
+
リデューサを使えば、コンポーネントの state 更新ロジックを集約することができます。コンテクストを使えば、他のコンポーネントに深く情報を渡すことができます。そしてリデューサとコンテクストを組み合わせることで、複雑な画面の state 管理ができるようになります。
8
8
9
9
</Intro>
10
10
11
11
<YouWillLearn>
12
12
13
-
*How to combine a reducer with context
14
-
*How to avoid passing state and dispatch through props
15
-
*How to keep context and state logic in a separate file
13
+
*リデューサとコンテクストを組み合わせる方法
14
+
* state とディスパッチ関数を props を介して渡すことを避ける方法
15
+
*コンテクストと state ロジックを別のファイルに保持する方法
16
16
17
17
</YouWillLearn>
18
18
19
-
## Combining a reducer with context {/*combining-a-reducer-with-context*/}
In this example from [the introduction to reducers](/learn/extracting-state-logic-into-a-reducer), the state is managed by a reducer. The reducer function contains all of the state update logic and is declared at the bottom of this file:
21
+
[リデューサの導入記事](/learn/extracting-state-logic-into-a-reducer)で紹介した以下の例では、state はリデューサによって管理されています。リデューサ関数はファイルの下部で宣言されており、そこに state の更新ロジックがすべて含まれています。
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.
210
+
リデューサを使うことで、イベントハンドラを短く簡潔に保てます。しかしアプリが大きくなるにつれ、別の困難が発生することがあります。**現在 `tasks` state と `dispatch`関数は、トップレベルの `TaskApp`コンポーネントでしか使えません**。他のコンポーネントがタスクリストの読み込みや変更をできるようにするには、現在の state や変更用のイベントハンドラを props として明示的に[下に渡していく](/learn/passing-props-to-a-component)必要があります。
211
211
212
-
For example, `TaskApp`passes a list of tasks and the event handlers to `TaskList`:
@@ -219,7 +219,7 @@ For example, `TaskApp` passes a list of tasks and the event handlers to `TaskLis
219
219
/>
220
220
```
221
221
222
-
And `TaskList`passes the event handlers to `Task`:
222
+
`TaskList`もイベントハンドラを `Task` に渡しています:
223
223
224
224
```js
225
225
<Task
@@ -229,30 +229,30 @@ And `TaskList` passes the event handlers to `Task`:
229
229
/>
230
230
```
231
231
232
-
In a small example like this, this works well, but if you have tens or hundreds of components in the middle, passing down all state and functions can be quite frustrating!
232
+
このような小さなサンプルではこれはうまく機能しますが、間に数十、数百といったコンポーネントがある場合、すべての state や関数をこのように渡していくのは非常に面倒です!
233
233
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".**
234
+
というわけで、`tasks` state と `dispatch`関数は、props 経由で渡すのではなく、[コンテクストに入れる](/learn/passing-data-deeply-with-context)方が望ましい場合があります。**こうすることで `TaskApp`ツリーの下部にある任意のコンポーネントが、"props の穴掘り作業 (prop drilling)" を繰り返さずともタスクのリストを読み取り、アクションをディスパッチすることができるようになります**。
235
235
236
-
Here is how you can combine a reducer with context:
236
+
以下がリデューサをコンテクストと組み合わせる方法です。
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.コンテクストを**作成する**。
239
+
2. state と dispatch をコンテクストに**入れる**。
240
+
3.ツリー内の任意の場所でコンテクストを**使用する**。
241
241
242
-
### Step 1: Create the context {/*step-1-create-the-context*/}
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:
@@ -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:
**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.
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:
This file is about to get crowded! You'll move the reducer into that same file. Then you'll declare a new `TasksProvider`component in the same file. This component will tie all the pieces together:
@@ -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
+
コンポーネントがコンテクストを読む必要がある場合、これらの関数を使うことができます。
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:**
You can think of `TasksProvider`as a part of the screen that knows how to deal with tasks, `useTasks`as a way to read them, and `useTasksDispatch`as a way to update them from any component below in the tree.
Functions like `useTasks`and`useTasksDispatch`are called *[Custom Hooks.](/learn/reusing-logic-with-custom-hooks)* Your function is considered a custom Hook if its name starts with `use`. This lets you use other Hooks, like `useContext`, inside it.
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.
0 commit comments