Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate react-without-es6 #53

Merged
merged 23 commits into from
Feb 4, 2019
Merged
Changes from 20 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
00d810d
translate react-without-es6
ginpei Feb 3, 2019
66629d5
found the Japanese expression
ginpei Feb 3, 2019
7979f50
misec
ginpei Feb 3, 2019
fc86c5a
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
d34be5c
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
488607b
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
3220bff
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
82928c8
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
4f284a4
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
c829bfd
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
0aa3d9d
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
2e3cc58
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
0acf07f
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
91d4dd2
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
5ec71c8
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
1b05824
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
2c7b689
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
8f04e4f
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
d874e5b
Update content/docs/react-without-es6.md
smikitky Feb 3, 2019
01948be
not waiting but wanting
ginpei Feb 3, 2019
b8a909a
Update content/docs/react-without-es6.md
koba04 Feb 4, 2019
6ce55af
Update content/docs/react-without-es6.md
koba04 Feb 4, 2019
ee1e08e
remove unnecessary cho-onpu
ginpei Feb 4, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 28 additions & 30 deletions content/docs/react-without-es6.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
id: react-without-es6
title: React Without ES6
title: ES6 なしで React を使う
permalink: docs/react-without-es6.html
---

Normally you would define a React component as a plain JavaScript class:
通常、React コンポーネントはプレーンな JavaScript クラスとして定義されます。

```javascript
class Greeting extends React.Component {
Expand All @@ -14,8 +14,7 @@ class Greeting extends React.Component {
}
```

If you don't use ES6 yet, you may use the `create-react-class` module instead:

もしあなたがまだ ES6 を使っていないのであれば、代わりに `create-react-class` モジュールを使うことができます。

```javascript
var createReactClass = require('create-react-class');
Expand All @@ -26,11 +25,11 @@ var Greeting = createReactClass({
});
```

The API of ES6 classes is similar to `createReactClass()` with a few exceptions.
ES6 のクラス API は `createReactClass()` とよく似ていますが、いくつかの例外があります。

## Declaring Default Props
## デフォルト props の宣言

With functions and ES6 classes `defaultProps` is defined as a property on the component itself:
関数や ES6 クラスでは、`defaultProps` はコンポーネント自体のプロパティとして定義されます。

```javascript
class Greeting extends React.Component {
Expand All @@ -42,7 +41,7 @@ Greeting.defaultProps = {
};
```

With `createReactClass()`, you need to define `getDefaultProps()` as a function on the passed object:
`createReactClass()` の場合、渡されるオブジェクト内の関数として `getDefaultProps()` を定義する必要があります。

```javascript
var Greeting = createReactClass({
Expand All @@ -57,9 +56,9 @@ var Greeting = createReactClass({
});
```

## Setting the Initial State
## state の初期値の設定

In ES6 classes, you can define the initial state by assigning `this.state` in the constructor:
ES6 クラスでは、コンストラクターで `this.state` へ代入することで state の初期値を定義できます。
ginpei marked this conversation as resolved.
Show resolved Hide resolved

```javascript
class Counter extends React.Component {
Expand All @@ -71,7 +70,7 @@ class Counter extends React.Component {
}
```

With `createReactClass()`, you have to provide a separate `getInitialState` method that returns the initial state:
`createReactClass()` の場合、state の初期値を返す `getInitialState` メソッドを別途用意しなければなりません。

```javascript
var Counter = createReactClass({
Expand All @@ -82,9 +81,9 @@ var Counter = createReactClass({
});
```

## Autobinding
## 自動バインド
ginpei marked this conversation as resolved.
Show resolved Hide resolved

In React components declared as ES6 classes, methods follow the same semantics as regular ES6 classes. This means that they don't automatically bind `this` to the instance. You'll have to explicitly use `.bind(this)` in the constructor:
ES6 クラスとして宣言された React コンポーネントでは、メソッドは通常の ES6 クラスと同様のセマンティクスに従います。つまり、`this` がそのインスタンスへ自動的にバインドされることはありません。コンストラクタで明示的に `.bind(this)` を利用する必要があります。

```javascript
class SayHello extends React.Component {
Expand All @@ -110,7 +109,7 @@ class SayHello extends React.Component {
}
```

With `createReactClass()`, this is not necessary because it binds all methods:
`createReactClass()` の場合は全てのメソッドがバインドされるため、明示的なバインドは必要ありません。

```javascript
var SayHello = createReactClass({
Expand All @@ -132,10 +131,9 @@ var SayHello = createReactClass({
});
```

This means writing ES6 classes comes with a little more boilerplate code for event handlers, but the upside is slightly better performance in large applications.

If the boilerplate code is too unattractive to you, you may enable the **experimental** [Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) syntax proposal with Babel:
これはつまり、ES6 クラスで書くとイベントハンドラーのための定型文が少し多くなってしまうということなのですが、一方では大きなアプリケーションの場合にわずかながらパフォーマンスが向上するという側面もあります。
ginpei marked this conversation as resolved.
Show resolved Hide resolved

この定型文的コードがあまりに醜く感じられる場合、Babel を使って**実験的**な [Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) 構文提案を有効にするとよいかもしれません。

```javascript
class SayHello extends React.Component {
Expand All @@ -159,27 +157,27 @@ class SayHello extends React.Component {
}
```

Please note that the syntax above is **experimental** and the syntax may change, or the proposal might not make it into the language.
上記の構文は**実験的**なものであり、構文が変わるかもしれないこと、あるいは言語に取り入れられないかもしれないことに留意してください。

If you'd rather play it safe, you have a few options:
安全にやりたい場合は他の選択肢もあります。

* Bind methods in the constructor.
* Use arrow functions, e.g. `onClick={(e) => this.handleClick(e)}`.
* Keep using `createReactClass`.
* コンストラクターでメソッドをバインドする。
* 例えば `onClick={(e) => this.handleClick(e)}` のような形でアロー関数を利用する。
* 引き続き `createReactClass` を利用する。

## Mixins
## ミックスイン

>**Note:**
>**補足:**
>
>ES6 launched without any mixin support. Therefore, there is no support for mixins when you use React with ES6 classes.
>ES6 はミックスインのサポートを含んでいません。従って、React ES6 クラスで使う場合にミックスインのサポートはありません。
>
>**We also found numerous issues in codebases using mixins, [and don't recommend using them in the new code](/blog/2016/07/13/mixins-considered-harmful.html).**
>**加えて、ミックスインを用いたコードによる多くの問題が見つかっており、[新規コードで利用することは推奨されません](/blog/2016/07/13/mixins-considered-harmful.html)**
>
>This section exists only for the reference.
>この節は参考のためだけに存在します。

Sometimes very different components may share some common functionality. These are sometimes called [cross-cutting concerns](https://en.wikipedia.org/wiki/Cross-cutting_concern). `createReactClass` lets you use a legacy `mixins` system for that.
時には同じ機能が全く異なるコンポーネント間で共有されることがあります。これは[横断的関心事 (cross-cutting concerns)](https://en.wikipedia.org/wiki/Cross-cutting_concern) と呼ばれることがあります。 `createReactClass` であれば、横断的関心事のためにレガシーな `mixins` 機能を使うことができます。

One common use case is a component wanting to update itself on a time interval. It's easy to use `setInterval()`, but it's important to cancel your interval when you don't need it anymore to save memory. React provides [lifecycle methods](/docs/react-component.html#the-component-lifecycle) that let you know when a component is about to be created or destroyed. Let's create a simple mixin that uses these methods to provide an easy `setInterval()` function that will automatically get cleaned up when your component is destroyed.
よくある利用例のひとつは、一定時間ごとに自分自身を更新するコンポーネントです。`setInterval()` を使うのは簡単ですが、その場合はメモリ節約のため、コンポーネントが不要になった際にキャンセルすることが重要です。React は[ライフサイクルメソッド](/docs/react-component.html#the-component-lifecycle)を提供しており、コンポーネントが生成、破棄されるときに知らせてくれます。次のようなシンプルなミックスインを作ってみましょう。このミックスインのメソッドは簡単な `setInterval()` 機能を提供し、コンポーネントが破棄されるタイミングで自動的にクリーンアップされます。

```javascript
var SetIntervalMixin = {
Expand Down Expand Up @@ -222,4 +220,4 @@ ReactDOM.render(
);
```

If a component is using multiple mixins and several mixins define the same lifecycle method (i.e. several mixins want to do some cleanup when the component is destroyed), all of the lifecycle methods are guaranteed to be called. Methods defined on mixins run in the order mixins were listed, followed by a method call on the component.
コンポーネントが複数のミックスインを使っており、いくつかのミックスインが同じライフサイクルメソッドを定義している場合(例:コンポーネント破棄時に複数のミックスインがクリーンアップ処理をする)、全てのライフサイクルメソッドが呼ばれることが保証されています。ミックスインで定義されているメソッドはミックスインとして列挙された順に実行され、そのあとにコンポーネントのメソッドが呼ばれます。
ginpei marked this conversation as resolved.
Show resolved Hide resolved