Skip to content

Commit 1b5e1c5

Browse files
committed
wip
1 parent 97cb8d8 commit 1b5e1c5

File tree

1 file changed

+38
-38
lines changed

1 file changed

+38
-38
lines changed

Diff for: content/docs/higher-order-components.md

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
11
---
22
id: higher-order-components
3-
title: Higher-Order Components
3+
title: 고차 컴포넌트
44
permalink: docs/higher-order-components.html
55
---
66

7-
A higher-order component (HOC) is an advanced technique in React for reusing component logic. HOCs are not part of the React API, per se. They are a pattern that emerges from React's compositional nature.
7+
고차 컴포넌트(HOC, Higher Order Component)는 React에서 컴포넌트 로직을 재사용하기 위한 고급 기술입니다. HOC React API의 일부분이 아니라 React의 React 컴포넌트의 성격으로 부터 자연스럽게 등장한 패턴입니다.
88

9-
Concretely, **a higher-order component is a function that takes a component and returns a new component.**
9+
구체적으로 고차 컴포넌트는 **컴포넌트를 받아 새로운 컴포넌트를 반환하는 함수입니다.**
1010

1111
```js
1212
const EnhancedComponent = higherOrderComponent(WrappedComponent);
1313
```
1414

15-
Whereas a component transforms props into UI, a higher-order component transforms a component into another component.
15+
컴포넌트가 UI를 props로 변환하는 반면에 고차 컴포넌트는 컴포넌트를 다른 컴포넌트로 변환합니다.
1616

17-
HOCs are common in third-party React libraries, such as Redux's [`connect`](https://github.com/reduxjs/react-redux/blob/master/docs/api/connect.md#connect) and Relay's [`createFragmentContainer`](http://facebook.github.io/relay/docs/en/fragment-container.html).
17+
고차 컴포넌트는 Redux의 [`connect`](https://github.com/reduxjs/react-redux/blob/master/docs/api/connect.md#connect) 나 Relay의 [`createFragmentContainer`](http://facebook.github.io/relay/docs/en/fragment-container.html) 같은 React 서드파티 라이브러리에서 자주 사용됩니다.
1818

19-
In this document, we'll discuss why higher-order components are useful, and how to write your own.
19+
이 문서에서 고차 컴포넌트가 왜 유용한 지 이야기하고 어떻게 작성하는 지 설명합니다.
2020

21-
## Use HOCs For Cross-Cutting Concerns {#use-hocs-for-cross-cutting-concerns}
21+
## 크로스 커팅 문제(Cross-Cutting Concerns)에 고차 컴포넌트 사용하기 {#use-hocs-for-cross-cutting-concerns}
2222

23-
> **Note**
23+
> **주의**
2424
>
25-
> We previously recommended mixins as a way to handle cross-cutting concerns. We've since realized that mixins create more trouble than they are worth. [Read more](/blog/2016/07/13/mixins-considered-harmful.html) about why we've moved away from mixins and how you can transition your existing components.
25+
> 이전에는 크로스 커팅 문제를 제어하기 위해 mixin 사용을 권장했습니다. 하지만 mixin의 사용으로 얻는 이점보다 더 많은 문제를 일으킨다는 것을 알게되었습니다.. 우리가 mixin을 더 이상 권장하지않는 이유와 기존 컴포넌트를 어떻게 변환하는 지에 대해서 [이 글](/blog/2016/07/13/mixins-considered-harmful.html)을 읽어보세요.
2626
27-
Components are the primary unit of code reuse in React. However, you'll find that some patterns aren't a straightforward fit for traditional components.
27+
컴포넌트는 React에서 코드 재사용의 기본 단위입니다. 그러나 일부 패턴에서 컴포넌트의 재사용은 잘 어울리지 않을 수 있습니다.
2828

29-
For example, say you have a `CommentList` component that subscribes to an external data source to render a list of comments:
29+
외부로부터 데이터를 구독하여 댓글 목록을 렌더링하는 `CommentList` 컴포넌트로 예를 들겠습니다.
3030

3131
```js
3232
class CommentList extends React.Component {
3333
constructor(props) {
3434
super(props);
3535
this.handleChange = this.handleChange.bind(this);
3636
this.state = {
37-
// "DataSource" is some global data source
37+
// "DataSource" 는 글로벌 데이터 소스로 정의되어 있습니다.
3838
comments: DataSource.getComments()
3939
};
4040
}
4141

4242
componentDidMount() {
43-
// Subscribe to changes
43+
// 변화감지를 위해 리스너를 추가합니다.
4444
DataSource.addChangeListener(this.handleChange);
4545
}
4646

4747
componentWillUnmount() {
48-
// Clean up listener
48+
// 리스너를 제거합니다.
4949
DataSource.removeChangeListener(this.handleChange);
5050
}
5151

5252
handleChange() {
53-
// Update component state whenever the data source changes
53+
// 데이터 소스가 변경될때 마다 comments를 업데이트합니다.
5454
this.setState({
5555
comments: DataSource.getComments()
5656
});
@@ -68,7 +68,7 @@ class CommentList extends React.Component {
6868
}
6969
```
7070

71-
Later, you write a component for subscribing to a single blog post, which follows a similar pattern:
71+
그리고 블로그 포스트를 구독하기 위해 위와 비슷한 패턴으로 컴포넌트를 작성합니다.
7272

7373
```js
7474
class BlogPost extends React.Component {
@@ -100,15 +100,15 @@ class BlogPost extends React.Component {
100100
}
101101
```
102102

103-
`CommentList` and `BlogPost` aren't identical — they call different methods on `DataSource`, and they render different output. But much of their implementation is the same:
103+
`CommentList` `BlogPost` 컴포넌트는 동일하지 않습니다. 두 컴포넌트는 `DataSource` 에서 서로 다른 메서드를 호출하며 다른 렌더링 결과를 보여줍니다. 하지만 대부분의 구현체는 동일합니다.
104104

105-
- On mount, add a change listener to `DataSource`.
106-
- Inside the listener, call `setState` whenever the data source changes.
107-
- On unmount, remove the change listener.
105+
- 컴포넌트가 마운트되면, change 리스너를 `DataSource` 에 추가합니다.
106+
- 리스너 안에서, 데이터 소스가 변경되면 `setState` 를 호출합니다.
107+
- 컴포넌트가 unmount되면 change 리스너를 제거합니다.
108108

109-
You can imagine that in a large app, this same pattern of subscribing to `DataSource` and calling `setState` will occur over and over again. We want an abstraction that allows us to define this logic in a single place and share it across many components. This is where higher-order components excel.
109+
규모가 큰 어플리케이션에서 `DataSource` 를 구독하고 `setState` 를 호출하는 동일한 패턴이 반복적으로 발생한다면, 이 로직을 한 곳에서 정의하고 많은 컴포넌트에서 로직을 공유할 수 있게하는 추상화가 필요하게됩니다. 이 경우에 고차 컴포넌트를 사용하면 좋습니다.
110110

111-
We can write a function that creates components, like `CommentList` and `BlogPost`, that subscribe to `DataSource`. The function will accept as one of its arguments a child component that receives the subscribed data as a prop. Let's call the function `withSubscription`:
111+
`DataSource` 를 구독하는 `CommentList` `BlogPost` 같은 컴포넌트를 생성하는 함수를 작성할 수 있습니다. 데이터를 prop으로 구독하여 전달받는 자식 컴포넌트를 파라미터 중 하나로 받는 함수를 만듭니다. 이 함수를 `withSubscription` 라고 합시다.
112112

113113
```js
114114
const CommentListWithSubscription = withSubscription(
@@ -122,14 +122,14 @@ const BlogPostWithSubscription = withSubscription(
122122
);
123123
```
124124

125-
The first parameter is the wrapped component. The second parameter retrieves the data we're interested in, given a `DataSource` and the current props.
125+
첫번째 파라미터는 래핑된 컴포넌트입니다. 두번째 파라미터에는 `DataSource`와 현재 props를 가지고 컴포넌트에서 관심있는 데이터를 검색합니다.
126126

127-
When `CommentListWithSubscription` and `BlogPostWithSubscription` are rendered, `CommentList` and `BlogPost` will be passed a `data` prop with the most current data retrieved from `DataSource`:
127+
`CommentListWithSubscription` `BlogPostWithSubscription` 가 렌더링될 때 `CommentList` `BlogPost` `DataSource` 에서 가장 최근에 검색된 데이터를 `data` prop으로 전달합니다.
128128

129129
```js
130-
// This function takes a component...
130+
// 이 함수는 컴포넌트를 매개변수로 받고...
131131
function withSubscription(WrappedComponent, selectData) {
132-
// ...and returns another component...
132+
// ...다른 컴포넌트를 반환하는데...
133133
return class extends React.Component {
134134
constructor(props) {
135135
super(props);
@@ -140,7 +140,7 @@ function withSubscription(WrappedComponent, selectData) {
140140
}
141141

142142
componentDidMount() {
143-
// ... that takes care of the subscription...
143+
// ... 구독을 담당하고...
144144
DataSource.addChangeListener(this.handleChange);
145145
}
146146

@@ -155,25 +155,25 @@ function withSubscription(WrappedComponent, selectData) {
155155
}
156156

157157
render() {
158-
// ... and renders the wrapped component with the fresh data!
159-
// Notice that we pass through any additional props
158+
// ... 래핑된 컴포넌트를 새로운 데이터로 랜더링 합니다!
159+
// 컴포넌트에 추가로 props를 내려주는 것에 주목하세요.
160160
return <WrappedComponent data={this.state.data} {...this.props} />;
161161
}
162162
};
163163
}
164164
```
165+
고차 컴포넌트는 입력된 컴포넌트를 수정하지 않으며 상속을 사용하여 동작을 복사하지도 않습니다. 오히려 고차 컴포넌트는 원래 컴포넌트를 컨테이너 컴포넌트에 *래핑하여* *조정합니다*. 고차 컴포넌트는 사이드 이펙트가 전혀 없는 순수 함수입니다.
165166

166-
Note that a HOC doesn't modify the input component, nor does it use inheritance to copy its behavior. Rather, a HOC *composes* the original component by *wrapping* it in a container component. A HOC is a pure function with zero side-effects.
167+
이게 전부입니다. 래핑된 컴포넌트는 출력물을 렌더링하는 데 사용되는 `data`, 새로운 prop을 포함한 컨테이너의 모든 props를 전달받습니다. 고차 컴포넌트는 데이터가 사용되는 이유 및 방법과 연관이 없으며 래핑된 컴포넌트는 데이터가 어디서부터 왔는지와 관련 없습니다.
167168

168-
And that's it! The wrapped component receives all the props of the container, along with a new prop, `data`, which it uses to render its output. The HOC isn't concerned with how or why the data is used, and the wrapped component isn't concerned with where the data came from.
169+
`withSubscription` 는 일반 함수이기때문에 원하는 갯수의 인수를 추가할 수 있습니다. 예를 들어 래핑된 컴포넌트로부터 고차 컴포넌트를 더 격리시키기 위해 `data` prop 이름을 설정 가능하게 만들 수 있습니다. 혹은 `shouldComponentUpdate` 설정을 위한 인수를 받게 하거나 데이터 소스를 설정하는 인수를 받게할 수도 있습니다. 고차 컴포넌트가 컴포넌트 정의 방법을 완전히 제어할 수 있기 때문에 이런 작업들이 모두 가능합니다.
169170

170-
Because `withSubscription` is a normal function, you can add as many or as few arguments as you like. For example, you may want to make the name of the `data` prop configurable, to further isolate the HOC from the wrapped component. Or you could accept an argument that configures `shouldComponentUpdate`, or one that configures the data source. These are all possible because the HOC has full control over how the component is defined.
171+
컴포넌트와 마찬가지로 withSubscription 과 래핑된 컴포넌트 간 맺음(contract)은 완전히 props 기반입니다. 이렇게하면 래핑된 컴포넌트에 동일한 props를 제공한다면 다른 고차 컴포넌트를 쉽게 교차할 수 있습니다. 예를 들어 데이터를 가져오는 라이브러리를 변경하는 경우 유용하게 사용할 수 있습니다.
171172

172-
Like components, the contract between `withSubscription` and the wrapped component is entirely props-based. This makes it easy to swap one HOC for a different one, as long as they provide the same props to the wrapped component. This may be useful if you change data-fetching libraries, for example.
173+
## 원본 컴포넌트를 변환하지마세요. Composition을 사용하세요.
174+
. {#dont-mutate-the-original-component-use-composition}
173175

174-
## Don't Mutate the Original Component. Use Composition. {#dont-mutate-the-original-component-use-composition}
175-
176-
Resist the temptation to modify a component's prototype (or otherwise mutate it) inside a HOC.
176+
고차 컴포넌트 내부에서 컴포넌트의 프로토타입을 수정(또는 변형)하지 않도록 합니다.
177177

178178
```js
179179
function logProps(InputComponent) {
@@ -186,11 +186,11 @@ function logProps(InputComponent) {
186186
return InputComponent;
187187
}
188188

189-
// EnhancedComponent will log whenever props are received
189+
// EnhancedComponent 는 props를 받을때 마다 log를 남깁니다.
190190
const EnhancedComponent = logProps(InputComponent);
191191
```
192192

193-
There are a few problems with this. One is that the input component cannot be reused separately from the enhanced component. More crucially, if you apply another HOC to `EnhancedComponent` that *also* mutates `componentWillReceiveProps`, the first HOC's functionality will be overridden! This HOC also won't work with function components, which do not have lifecycle methods.
193+
여기에는 몇가지 문제점이 있습니다. 하나는 입력 컴포넌트를 개선된 컴포넌트와 별도로 재사용할 수 없다는 것입니다. 더 중요한 것은 `EnhancedComponent에` 또 다른 고차 컴포넌트를 적용하여 `componentWillReceiveProps` *또한* 변형시키면 첫번째 고차 컴포넌트의 기능이 오버라이드됩니다. 이 고차 컴포넌트는 라이프사이클 메서드가 없는 함수 컴포넌트에서는 동작하지않습니다.
194194

195195
Mutating HOCs are a leaky abstraction—the consumer must know how they are implemented in order to avoid conflicts with other HOCs.
196196

0 commit comments

Comments
 (0)