Skip to content

Translate React Without ES6 #177

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

Merged
merged 11 commits into from
Mar 1, 2019
70 changes: 34 additions & 36 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: React без ES6
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.
API ES6-классов похож на `createReactClass()` за некоторыми исключениями.

## Declaring Default Props {#declaring-default-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 {#setting-the-initial-state}
## Установка начального состояния

In ES6 classes, you can define the initial state by assigning `this.state` in the constructor:
В ES6-классах вы можете определять начальное состояние через `this.state` в конструкторе:

```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()` вам придется отдельно реализовать метод `getInitialState`, который возвращает начальное состояние:

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

## Autobinding {#autobinding}
## Автоматическая привязка

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:
В компонентах React, объявленных как классы ES6, методы следуют той же семантике, что и обычные классы ES6. Это означает, что они сами по себе не связывают `this` с экземпляром. Вам придется явно использовать `.bind(this)` в конструкторе:

```javascript
class SayHello extends React.Component {
constructor(props) {
super(props);
this.state = {message: 'Hello!'};
// This line is important!
// Эта строка важна!
this.handleClick = this.handleClick.bind(this);
}

Expand All @@ -100,7 +99,7 @@ class SayHello extends React.Component {
}

render() {
// Because `this.handleClick` is bound, we can use it as an event handler.
// Мы можем использовать `this.handleClick` как обработчик событий, потому что он привязан
return (
<button onClick={this.handleClick}>
Say hello
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,19 +131,18 @@ 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 пишутся с чуть более универсальным кодом для обработчиков событий, при этом производительность громоздких приложений немного выше.

Если универсальный код для вас слишком неприглядный, вы можете включить **экспериментальный** [Class Properties](https://babeljs.io/docs/plugins/transform-class-properties/) синтаксис от Babel:

```javascript
class SayHello extends React.Component {
constructor(props) {
super(props);
this.state = {message: 'Hello!'};
}
// WARNING: this syntax is experimental!
// Using an arrow here binds the method:
// ВНИМАНИЕ! Этот синтаксис экспериментальный!
// Использование стрелки здесь связывает метод:
handleClick = () => {
alert(this.state.message);
}
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 {#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.
Иногда очень разные компоненты могут иметь общую функциональность. Эти проблемы иногда называют [межсекторальными](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 All @@ -197,12 +195,12 @@ var SetIntervalMixin = {
var createReactClass = require('create-react-class');

var TickTock = createReactClass({
mixins: [SetIntervalMixin], // Use the mixin
mixins: [SetIntervalMixin], // Использовать примешивание
getInitialState: function() {
return {seconds: 0};
},
componentDidMount: function() {
this.setInterval(this.tick, 1000); // Call a method on the mixin
this.setInterval(this.tick, 1000); // Вызвать метод на примешивании
},
tick: function() {
this.setState({seconds: this.state.seconds + 1});
Expand All @@ -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.
Если компонент использует несколько примешиваний и они определяют один и тот же метод жизненного цикла (т.е. хотят выполнить некоторую очистку при уничтожении компонента), все методы жизненного цикла гарантированно будут вызваны. Методы, определенные на примешиваниях, запускаются по порядку, после вызова метода на компоненте.