State
아래와 같이 constructor를 사용하고, this.state로 null을 만들어준다.
constructor(props) {
super(props);
this.state = {
value: null,
}
}
아래와 같이, square를 눌렀을때 setState
를 이용하여, 'X'라는 값 넣어주기(null -> 'X'로)
<button className="square" onClick={() => { this.setState({value: 'X'})}}>
아래와 같이 this.state.value - this에 state를 붙이고 사용하기
render() {
return(
<button className="square" onClick={() => {
this.setState({value: 'X'})}}>
{this.state.value}
</button>
)
}
이 글은 저에게 많은 도움이 되었습니다.