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
<pdata-height="600"data-theme-id="0"data-slug-hash="qPrNQZ"data-default-tab="js"data-user="lacker"data-embed-version="2"class="codepen">See the Pen <ahref="https://codepen.io/gaearon/pen/qPrNQZ">Thinking In React: Step 4</a> on <ahref="http://codepen.io">CodePen</a>.</p>
111
111
112
-
OK, so we've identified what the minimal set of app state is. Next, we need to identify which component mutates, or *owns*, this state.
112
+
さて、state の最小構成が明確になりました。次は、どのコンポーネントを変化させるのか、そして*どのコンポーネントが state を持つのか*を明確にしましょう。
113
113
114
-
Remember: React is all about one-way data flow down the component hierarchy. It may not be immediately clear which component should own what state. **This is often the most challenging part for newcomers to understand,**so follow these steps to figure it out:
114
+
復習:React は、コンポーネントの階層構造をデータが下っていく、単方向データフローで成り立っています。もしかすると、どのコンポーネントがどんな state を持つべきなのかが、すぐにはわからないかもしれません。**これは、初学者がReactへの理解を深める上で、最も難しい問題になりがちなところ**なので、ステップを踏みながら理解していきましょう。
115
115
116
-
For each piece of state in your application:
116
+
アプリの各 state について、次の各項目を確認していきます。
117
117
118
-
*Identify every component that renders something based on that state.
119
-
*Find a common owner component (a single component above all the components that need the state in the hierarchy).
120
-
*Either the common owner or another component higher up in the hierarchy should own the state.
121
-
*If you can't find a component where it makes sense to own the state, create a new component simply for holding the state and add it somewhere in the hierarchy above the common owner component.
118
+
*その state を使って表示を行う、すべてのコンポーネントを確認する
119
+
*共通の親コンポーネントを見つける(その階層構造の中で、ある state を必要としているすべてのコンポーネントの真上にある単一のコンポーネントのことです)
120
+
*共通の親コンポーネントか、その階層構造でさらに上位の別のコンポーネントが state を持っているべきである
121
+
*もし state を持つにふさわしいコンポーネントを見つけられなかった場合は、state を保持するためのコンポーネントを作り、そこに state を置いた上で、階層構造の中ですでに見つけていた共通の親コンポーネントの上に配置する
122
122
123
-
Let's run through this strategy for our application:
123
+
それでは、この戦術をサンプルアプリにも適用してみましょう。
124
124
125
-
*`ProductTable`needs to filter the product list based on state and `SearchBar`needs to display the search text and checked state.
126
-
*The common owner component is `FilterableProductTable`.
127
-
*It conceptually makes sense for the filter text and checked value to live in `FilterableProductTable`
Cool, so we've decided that our state lives in `FilterableProductTable`. First, add an instance property `this.state = {filterText: '', inStockOnly: false}`to`FilterableProductTable`'s `constructor`to reflect the initial state of your application. Then, pass `filterText`and`inStockOnly`to`ProductTable`and`SearchBar`as a prop. Finally, use these props to filter the rows in `ProductTable`and set the values of the form fields in `SearchBar`.
You can start seeing how your application will behave: set `filterText`to`"ball"`and refresh your app. You'll see that the data table is updated correctly.
0 commit comments