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
In this section, we will consider a few problems where developers new to React often reach for inheritance, and show how we can solve them with composition.
Some components don't know their children ahead of time. This is especially common for components like `Sidebar`or`Dialog`that represent generic "boxes".
This lets other components pass arbitrary children to them by nesting the JSX:
31
+
これにより他のコンポーネントから JSX をネストすることで任意の子要素を渡すことができます。
32
32
33
33
```js{4-9}
34
34
function WelcomeDialog() {
@@ -47,9 +47,9 @@ function WelcomeDialog() {
47
47
48
48
**[Try it on CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
49
49
50
-
Anything inside the `<FancyBorder>` JSX tag gets passed into the `FancyBorder`component as a `children`prop. Since `FancyBorder`renders `{props.children}` inside a `<div>`, the passed elements appear in the final output.
While this is less common, sometimes you might need multiple "holes" in a component. In such cases you may come up with your own convention instead of using `children`:
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)
82
82
83
-
React elements like `<Contacts />`and`<Chat />`are just objects, so you can pass them as props like any other data. This approach may remind you of "slots" in other libraries but there are no limitations on what you can pass as props in React.
Sometimes we think about components as being "special cases" of other components. For example, we might say that a `WelcomeDialog` is a special case of `Dialog`.
86
+
## 特化したコンポーネント (Specialization)
88
87
89
-
In React, this is also achieved by composition, where a more "specific" component renders a more "generic" one and configures it with props:
[**Try it on CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
115
116
116
-
Composition works equally well for components defined as classes:
117
+
コンポジションはクラスとして定義されたコンポーネントでも同じように動作します。
117
118
118
119
```js{10,27-31}
119
120
function Dialog(props) {
@@ -163,10 +164,10 @@ class SignUpDialog extends React.Component {
163
164
164
165
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
165
166
166
-
## So What About Inheritance?
167
+
## 継承はどうするの?
167
168
168
-
At Facebook, we use React in thousands of components, and we haven't found any use cases where we would recommend creating component inheritance hierarchies.
Props and composition give you all the flexibility you need to customize a component's look and behavior in an explicit and safe way. Remember that components may accept arbitrary props, including primitive values, React elements, or functions.
If you want to reuse non-UI functionality between components, we suggest extracting it into a separate JavaScript module. The components may import it and use that function, object, or a class, without extending it.
0 commit comments