class 컴포넌트의 생명주기 (Life Cycle)
- 컴포넌트의 생명주기는 컴포넌트가 브라우저에 나타나고 업데이트 되고, 사라지게 될 때 호출되는 메서드 이다.
- 쉽게 말해, 특정 시점에 코드가 실행되도록 설정할 수 있다는 것.
- 메서드에 대해 간략히 요약 한다면,
- 그리기 ->
render
인풋창 그리기.
- 그리고 난 뒤 ->
componentDidMount
포커스 깜빡거리기.
- 그리고 난 뒤 변경 ->
componentDidUpdate
- 그리고 난 뒤 사라짐 ->
componentWillUnmount
ex)
import {Component} from 'react'
import Router from 'react'
export default class ClassCounterPage extends Component{
state = {
count : 0,
}
componentDidMout(){
console.log("마운트 됨")
}
componentDidUpdate(){
console.log("수정하고 다시 그림!!")
}
componentWillUnmount(){
console.log("여기서 나갈래요")
}
onClickCouter = ()=>{
console.log(this.state.conut)
this.setState(((prev))=>({
count : this.state.count(=prev.count) +1
}))
}
onClickMove = () => {
router.push('/')
}
render(){
return(
<div>
<div>현재 카운트 : {this.state.count}</div>
<button onClick={this.onClickCouter}>카운트 올리기</button>
<button onClick={this.onClickMove}>나가기</button>
</div>
)
}
}