Skip to content

Commit 7adb904

Browse files
authored
Merge pull request #5 from Naturalclar/translate/composition-vs-inheritance
Translate composition-vs-inheritance
2 parents 99b12e3 + 74a52df commit 7adb904

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

content/docs/composition-vs-inheritance.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
22
id: composition-vs-inheritance
3-
title: Composition vs Inheritance
3+
title: コンポジション vs 継承
44
permalink: docs/composition-vs-inheritance.html
55
redirect_from:
66
- "docs/multiple-components.html"
77
prev: lifting-state-up.html
88
next: thinking-in-react.html
99
---
1010

11-
React has a powerful composition model, and we recommend using composition instead of inheritance to reuse code between components.
11+
React は強力なコンポジションモデルを備えており、コンポーネント間のコードの再利用には継承よりもコンポジションをお勧めしています。
1212

13-
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.
13+
この章では、React を始めて間もない開発者が継承に手を出した時に陥りがちないくつかの問題と、コンポジションによりその問題がどのように解決できるのかについて考えてみます。
1414

15-
## Containment
15+
## 子要素の出力 (Containment)
1616

17-
Some components don't know their children ahead of time. This is especially common for components like `Sidebar` or `Dialog` that represent generic "boxes".
17+
コンポーネントの中には事前には子要素を知らないものもあります。これは `Sidebar` `Dialog` のような汎用的な "入れ物" をあらわすコンポーネントではよく使われています。
1818

19-
We recommend that such components use the special `children` prop to pass children elements directly into their output:
19+
このようなコンポーネントでは特別な `children` という props を使い、以下のようにして受け取った子要素を出力することができます。
2020

2121
```js{4}
2222
function FancyBorder(props) {
@@ -28,7 +28,7 @@ function FancyBorder(props) {
2828
}
2929
```
3030

31-
This lets other components pass arbitrary children to them by nesting the JSX:
31+
これにより他のコンポーネントから JSX をネストすることで任意の子要素を渡すことができます。
3232

3333
```js{4-9}
3434
function WelcomeDialog() {
@@ -47,9 +47,9 @@ function WelcomeDialog() {
4747

4848
**[Try it on CodePen](https://codepen.io/gaearon/pen/ozqNOV?editors=0010)**
4949

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.
50+
`<FancyBorder>` JSX タグの内側のあらゆる要素は `FancyBorder` `children` という props として渡されます。 `FancyBorder` `<div>` の内側に `{props.children}` をレンダリングするので、渡された要素が出力されます。
5151

52-
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`:
52+
あまり一般的ではありませんが、複数の箇所に子要素を追加したいケースも考えられます。そのようなケースでは以下のように `children` の props の代わりに独自の props を作成して渡すことができます。
5353

5454
```js{5,8,18,21}
5555
function SplitPane(props) {
@@ -80,13 +80,14 @@ function App() {
8080

8181
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZOJp?editors=0010)
8282

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.
83+
`<Contacts />` `<Chat />` のような React の要素はただのオブジェクトなので、他のあらゆるデータと同様に props として渡すことができます。このアプローチは他のライブラリで言うところの slot に似ていると感じるかもしれませんが、React のコンポーネントに props として渡せるものに制限はありません。
8484

85-
## Specialization
8685

87-
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)
8887

89-
In React, this is also achieved by composition, where a more "specific" component renders a more "generic" one and configures it with props:
88+
コンポーネントを他のコンポーネントの "特別なケース" として考えることがあります。例えば、`WelcomeDialog``Dialog` の特別なケースと言えるでしょう。
89+
90+
React ではこれもコンポジションで実現できます。汎用的なコンポーネントに props を渡して設定することで、より特化したコンポーネントを作成することができます。
9091

9192
```js{5,8,16-18}
9293
function Dialog(props) {
@@ -113,7 +114,7 @@ function WelcomeDialog() {
113114

114115
[**Try it on CodePen**](https://codepen.io/gaearon/pen/kkEaOZ?editors=0010)
115116

116-
Composition works equally well for components defined as classes:
117+
コンポジションはクラスとして定義されたコンポーネントでも同じように動作します。
117118

118119
```js{10,27-31}
119120
function Dialog(props) {
@@ -163,10 +164,10 @@ class SignUpDialog extends React.Component {
163164

164165
[**Try it on CodePen**](https://codepen.io/gaearon/pen/gwZbYa?editors=0010)
165166

166-
## So What About Inheritance?
167+
## 継承はどうするの?
167168

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.
169+
Facebook では、何千というコンポーネントで React を使用していますが、コンポーネント継承による階層構造が推奨されるケースは全く見つかっていません。
169170

170-
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.
171+
props とコンポジションにより、コンポーネントの見た目と振る舞いを明示的かつ安全にカスタマイズするのに十分な柔軟性が得られます。コンポーネントはどのような props でも受け付けることができ、それはプリミティブ値でも、React 要素でも、あるいは関数であってもよい、ということに留意して下さい。
171172

172-
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.
173+
コンポーネント間で非 UI 機能を再利用したい場合は、それを別の JavaScript モジュールに抽出することをお勧めします。コンポーネントはその関数やオブジェクト、クラスなどを継承することなくインポートすることで使用することができるでしょう。

0 commit comments

Comments
 (0)