Files
freeCodeCamp/guide/russian/react/higher-order-components/index.md
Egor 346b0a7ce1 docs: update Russian translation for the whole React guide (#23636)
* docs: update translation for React guide in Russian

* fix: removed extra line before frontmatter block

* fix: removed extra line in frontmatter block

* fix: corrected frontmatter block

* fix: corrected localeTitle in frontmatter block

* Update index.md

* fix: corrected localeTitle for Installation

* Update index.md

* Update index.md
2018-11-22 22:49:48 +04:00

51 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: Higher-Order Components
localeTitle: Higher-Order Components
---
## Higher-Order Components
В React ***Higher-Order Components*** (HOC) - это функция, которая принимает компонент и возвращает новый компонент. Программисты используют HOC для обеспечения **повторного использования компонентной логики** .
Если вы использовали Redux `connect`, то это значит, что вы уже работали с Higher-Order Components.
Основная идея:
```jsx
const EnhancedComponent = enhance(WrappedComponent);
```
Куда:
* `enhance` - это Higher-Order Component;
* `WrappedComponent` - это компонент, который вы хотите улучшить; а также
* `EnhancedComponent` - это новый компонент.
Это может стать телом `enhance` HOC:
```jsx
function enhance(WrappedComponent) {
return class extends React.Component {
render() {
const extraProp = 'This is an injected prop!';
return (
<div className="Wrapper">
<WrappedComponent
{...this.props}
extraProp={extraProp}
/>
</div>
);
}
}
}
```
В этом случае `React.Component` `enhance` возвращает **anonymous class** который расширяет `React.Component` . Этот новый компонент выполняет три простых действия:
* Отрисовывание `WrappedComponent` в элементе `div` ;
* Передача собственных реквизитов для `WrappedComponent` ; а также
* Внедрение дополнительной поддержки для `WrappedComponent` .
HOC - это всего лишь образец, который использует силу композиционной природы Реакта. **Они добавляют функции к компоненту** . С ними вы можете многое сделать!
## Другие источники
* [React docs: Компоненты более высокого порядка](https://reactjs.org/docs/higher-order-components.html)