Skip to content

React без JSX #118

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 9 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion content/docs/nav.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
- id: react-without-es6
title: React Without ES6
- id: react-without-jsx
title: React Without JSX
title: React без JSX
- id: reconciliation
title: Reconciliation
- id: refs-and-the-dom
Expand Down
28 changes: 14 additions & 14 deletions content/docs/react-without-jsx.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
id: react-without-jsx
title: React Without JSX
title: React без JSX
permalink: docs/react-without-jsx.html
---

JSX is not a requirement for using React. Using React without JSX is especially convenient when you don't want to set up compilation in your build environment.
JSX не является обязательным для работы с React. React можно использовать без JSX. Это особенно удобно, когда вы не хотите настраивать транспиляцию в процессе сборки.

Each JSX element is just syntactic sugar for calling `React.createElement(component, props, ...children)`. So, anything you can do with JSX can also be done with just plain JavaScript.
Каждый JSX элемент -- это просто синтаксический сахар для вызова `React.createElement(component, props, ...children)`. Так что всё, что вы можете сделать при помощи JSX, может быть сделано на чистом JavaScript.

Например, вот код с JSX:

For example, this code written with JSX:

```js
class Hello extends React.Component {
render() {
return <div>Hello {this.props.toWhat}</div>;
return <div>Привет, {this.props.toWhat}</div>;
}
}

Expand All @@ -23,12 +24,12 @@ ReactDOM.render(
);
```

can be compiled to this code that does not use JSX:
Он может быть превращён в код без JSX:

```js
class Hello extends React.Component {
render() {
return React.createElement('div', null, `Hello ${this.props.toWhat}`);
return React.createElement('div', null, `Привет, ${this.props.toWhat}`);
}
}

Expand All @@ -38,22 +39,21 @@ ReactDOM.render(
);
```

If you're curious to see more examples of how JSX is converted to JavaScript, you can try out [the online Babel compiler](babel://jsx-simple-example).
Если вас интересуют другие примеры того, как JSX превращается в JavaScript, вы можете попробовать [онлайн-компилятор Babel](babel://jsx-simple-example).

The component can either be provided as a string, or as a subclass of `React.Component`, or a plain function for stateless components.
Компонент может быть представлен в виде строки, класса-наследника от `React.Component` или обычной функции в случае компонента без состояния.

If you get tired of typing `React.createElement` so much, one common pattern is to assign a shorthand:
Если вас утомляет печатать `React.createElement`, то распространённой практикой является создать сокращение:

```js
const e = React.createElement;

ReactDOM.render(
e('div', null, 'Hello World'),
e('div', null, 'Привет, мир!'),
document.getElementById('root')
);
```

If you use this shorthand form for `React.createElement`, it can be almost as convenient to use React without JSX.

Alternatively, you can refer to community projects such as [`react-hyperscript`](https://github.com/mlmorg/react-hyperscript) and [`hyperscript-helpers`](https://github.com/ohanhi/hyperscript-helpers) which offer a terser syntax.
Если вы примените эту сокращенную форму для `React.createElement`, то использование React без JSX станет почти таким же удобным, как вы привыкли.

Кроме того, вы можете посмотреть на такие проекты как [`react-hyperscript`](https://github.com/mlmorg/react-hyperscript) и [`hyperscript-helpers`](https://github.com/ohanhi/hyperscript-helpers), которые предлагают короткий синтаксис.