반응형
SMALL
3.2 Component Life Cycle
life cycle method
기본적으로 react가 component를 생성하고 없애는 것
mounting : component가 screen에 표시되는 것
- constructor() 호출
- render() 호출
- componentDidMount() : component가 처음 render됐다는 것을 알려줌 (render() 이후 적용)
updating : 사용자가 만든 함수에 의해 update (setState를 호출할떄마다 발생)
- render() 호출
- componentDidUpdate () 호출
(setState 호출 -> component호출 -> renfer 호출 -> 업데이트 완료되면 componentDidUpdate 호출)
unmounting : component가 죽는 것
- componentWillUnmount() : component가 떠날 떄 호출
class App extends React.Component
{
state={
count:0
};
add=()=>{
this.setState(current=>({count:current.count+1}));
};
minus=()=>{
this.setState(current=>({count:current.count-1}));
};
componentDidMount(){//: component가 처음 render됐다는 것을 알려줌 (render() 이후 적용)
console.log("component rendered");
}
componentDidUpdate(){ //component가 update됐을 때 호출
console.log("I just updated")
}
componentWillUnmount(){ //component가 떠날 때 호출
console.log("Goodbye");
}
render(){
console.log("I'm rendering");
return (
<div>
<h1>The number is {this.state.count}</h1>
<button onClick={this.add}>Add</button>
<button onClick={this.minus}>Minus</button>
</div>
);
}
}
반응형
LIST
'Web 강의 > ReactJS로 영화 웹 서비스 만들기' 카테고리의 다른 글
[노마드코더/ReactJS로 영화 웹 서비스 만들기]3.3 Planning the Movie Component (0) | 2022.03.18 |
---|---|
[노마드코더/ReactJS로 영화 웹 서비스 만들기]3.0 Class Components and State (0) | 2022.03.18 |
[노마드코더/ReactJS로 영화 웹 서비스 만들기] 3.1 All you need to know about State (0) | 2022.03.18 |
[노마드코더/ReactJS로 영화 웹 서비스 만들기]2.4 Protection with PropTypes (0) | 2022.03.18 |
[노마드코더/ReactJS로 영화 웹 서비스 만들기]2.3 map Recap (0) | 2022.03.18 |