Skip to content

Commit 6bdd13f

Browse files
committed
react without es6 이론 정리
1 parent 4406965 commit 6bdd13f

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

react-without-es6/Seogeurim/.gitkeep

Whitespace-only changes.

react-without-es6/Seogeurim/README.md

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# React Without ES6
2+
3+
JavaScript의 class 문법은 ES6 문법이다. React 컴포넌트를 정의할 때 이 class 문법을 사용한다면 다음과 같다.
4+
5+
```jsx
6+
class Greeting extends React.Component {
7+
render() {
8+
return <h1>Hello, {this.props.name}</h1>;
9+
}
10+
}
11+
```
12+
13+
하지만 아직 ES6를 사용하지 않는다면 `create-react-class` 모듈을 사용하여 다음과 같이 표현할 수 있다.
14+
15+
```jsx
16+
var createReactClass = require('create-react-class');
17+
18+
var Greeting = createReactClass({
19+
render: function () {
20+
return <h1>Hello, {this.props.name}</h1>;
21+
},
22+
});
23+
```
24+
25+
`createReactClass()`는 ES6 class와 유사하지만 몇가지 다른 점이 있는데, 그 점에 대해 알아본다.
26+
27+
## Declaring Default Props
28+
29+
컴포넌트의 property로 정의하던 `defaultProps`는,
30+
31+
```jsx
32+
class Greeting extends React.Component {
33+
// ...
34+
}
35+
36+
Greeting.defaultProps = {
37+
name: 'Mary',
38+
};
39+
```
40+
41+
`createReactClass()`에서는 `getDefaultProps` 메서드로 정의해주어야 한다.
42+
43+
```jsx
44+
var Greeting = createReactClass({
45+
getDefaultProps: function () {
46+
return {
47+
name: 'Mary',
48+
};
49+
},
50+
51+
// ...
52+
});
53+
```
54+
55+
## Setting the Initial State
56+
57+
ES6 class에서는 Initial state를 constructor에서 `this.state`에 할당하였지만,
58+
59+
```jsx
60+
class Counter extends React.Component {
61+
constructor(props) {
62+
super(props);
63+
this.state = { count: props.initialCount };
64+
}
65+
// ...
66+
}
67+
```
68+
69+
`createReactClass()`에서는 `getInitialState` 메서드로 정의해주어야 한다.
70+
71+
```jsx
72+
var Counter = createReactClass({
73+
getInitialState: function () {
74+
return { count: this.props.initialCount };
75+
},
76+
// ...
77+
});
78+
```
79+
80+
## Autobinding
81+
82+
ES6 class로 선언된 React 컴포넌트에서 메서드는 `this`를 컴포넌트 인스턴스에 자동으로 바인딩하지 않는다. 따라서 constructor에서 `.bind(this)`를 통해 바인딩 처리를 해줘야 한다.
83+
84+
```jsx
85+
class SayHello extends React.Component {
86+
constructor(props) {
87+
super(props);
88+
this.state = { message: 'Hello!' };
89+
90+
this.handleClick = this.handleClick.bind(this);
91+
}
92+
93+
handleClick() {
94+
alert(this.state.message);
95+
}
96+
97+
render() {
98+
return <button onClick={this.handleClick}>Say hello</button>;
99+
}
100+
}
101+
```
102+
103+
하지만 `createReactClass()`에서는 모든 메서드를 바인딩하기 때문에 그럴 필요가 없다.
104+
105+
```jsx
106+
var SayHello = createReactClass({
107+
getInitialState: function () {
108+
return { message: 'Hello!' };
109+
},
110+
111+
handleClick: function () {
112+
alert(this.state.message);
113+
},
114+
115+
render: function () {
116+
return <button onClick={this.handleClick}>Say hello</button>;
117+
},
118+
});
119+
```
120+
121+
즉 ES6 class를 사용해 이벤트 핸들러를 만드는 경우에 boilerplate 코드가 좀 더 많아진다. 하지만 규모가 커질수록 class를 사용하는 것이 약간 더 좋은 성능을 보인다. 이 boilerplate 코드가 마음에 안 든다면 화살표 함수를 사용하는 방법도 있다.

0 commit comments

Comments
 (0)