본문 바로가기

[React]

[React.js] state

www.inflearn.com/course/react-%EA%B0%95%EC%A2%8C-velopert/lecture/4143?tab=curriculum

 

html

1
<div id="root"></div>
cs


js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Counter extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      value:0
    };
    this.handleClick = this.handleClick.bind(this);
  }
  
  handleClick(){
      this.setState({
        value: this.state.value + 1
      });
    {/*
    아래 소스는 좋지 않음.
    this.state.value = this.state.valu +1;
    this.forceUpdate();
    */}
  }
  render(){
    return(
      <div>
        <h2>{this.state.value}</h2>
        <button onClick={this.handleClick}>Press Me</button>   {/*this.handleClick()  << 렌더링을 계속하게되서 오류 발생 */}
      </div>
    )
  }
}
 
class App extends React.Component {
  render() {
    return (
      <Counter/>
    );
  }
};
 
ReactDOM.render(
  <App></App>,
  document.getElementById("root")
);
cs

'[React]' 카테고리의 다른 글

[React Native] 참고 사이트  (0) 2021.04.06
[react-native] react-native run-android 실행 시 오류  (0) 2021.03.25
[react-native]참고 사이트  (0) 2021.03.25
[React.js] Component mapping  (0) 2021.02.16