// 구문
<child value = 'value' />
App.js
import React, { Component } from 'react';
import Counter from './Counter'; // Counter.js 불러오기(state내장하고 있는 콤포넌트)
class App extends Component {
render() {
return <counter/ >;
}
}
export default App;
Counter.js state사용된 콤포넌트
import React, { Component } from 'react';
class Counter extends Component {
state = {
number : 0 // 기본 state 지정. 변화시킬 값(여기서는 number)에 대해 지정
};
constructor(props) {
super(props);
this.handleIncrease = this.handleIncrease.bind(this);
this.handleDecrease = this.handleDecrease.bind(this);
}
handleIncrease = () => {
this.setState({
number : this.state.number + 1
});
};
handleDecrease = () => {
this.setState({
number : this.state.number - 1
});
};
render() {
return (
<div>
<h1>카운터</h1>
<div> 값 : {this.state.number}</div>
<button onClick = {this.handleIncrease}> + </button>
<button onClick = {this.handleDecrease}> - </button>
</div>
);
}
}
export default Counter;