리액트 컴포넌트는 탄생부터 죽음까지 여러 지점에서 개발자가 작업이 가능하도록 메서드를 오버라이딩 할 수 있게 해준다.
Initialization → Mounting → Updation(props/states) → Unmounting
constructor
conponentWillMount
render(최초랜더)
componentDidMount
<script type="text/babel">
console.log(React);
console.log(ReactDOM);
class App extends React.Component {
state = {
age: 39
}
constructor(props){
super(props);
console.log('constructor', props)
}
render(){
console.log('render');
return(
<div>
<h2>
Hello {this.props.name} - {this.state.age}
</h2>
</div>
);
}
componentWillMount(){
console.log('componentWillMount');
}
componentDidMount(){
console.log('componentDidMount');
setInterval(()=>{
// console.log("setInterval");
this.setState(state => ({...state, age: state.age + 1}));
}, 1000);
}
}
ReactDOM.render(<App name="Mark" />, document.querySelector('#root'))
</script>
componentWillReceiveProps
shouldComponentUpdate
componentWillUpdate
render
componentDidUpdate
<script type="text/babel">
console.log(React);
console.log(ReactDOM);
class App extends React.Component {
state = {
age: 39
}
constructor(props){
super(props);
console.log('constructor', props)
}
render(){
console.log('render');
return(
<div>
<h2>
Hello {this.props.name} - {this.state.age}
</h2>
</div>
);
}
componentWillMount(){
console.log('componentWillMount');
}
componentDidMount(){
console.log('componentDidMount');
setInterval(()=>{
// console.log("setInterval");
this.setState(state => ({...state, age: state.age + 1}));
}, 1000);
}
componentWillReceiveProps(nextProps){
console.log('componentWillReceiveProps', nextProps)
}
shouldComponentUpdate(nextProps, nextState){
console.log('shouldComponentUpdate', nextProps, nextState)
return true;
}
componentWillUpdate(nextProps, nextState){
console.log("componentWillUpdate", nextProps, nextState)
}
componentDidUpdate(prevProps, prevState){
console.log("componentDidUpdate", prevProps, prevState)
}
}
ReactDOM.render(<App name="Mark" />, document.querySelector('#root'))
</script>
componentWillUnmount
<script type="text/babel">
console.log(React);
console.log(ReactDOM);
class App extends React.Component {
state = {
age: 39
}
interval = null;
constructor(props){
super(props);
console.log('constructor', props)
}
render(){
console.log('render');
return(
<div>
<h2>
Hello {this.props.name} - {this.state.age}
</h2>
</div>
);
}
componentWillMount(){
console.log('componentWillMount');
}
componentDidMount(){
console.log('componentDidMount');
this.interval = setInterval(()=>{
// console.log("setInterval");
this.setState(state => ({...state, age: state.age + 1}));
}, 1000);
}
componentWillReceiveProps(nextProps){
console.log('componentWillReceiveProps', nextProps)
}
shouldComponentUpdate(nextProps, nextState){
console.log('shouldComponentUpdate', nextProps, nextState)
return true;
}
componentWillUpdate(nextProps, nextState){
console.log("componentWillUpdate", nextProps, nextState)
}
componentDidUpdate(prevProps, prevState){
console.log("componentDidUpdate", prevProps, prevState)
}
componentWillUnmount(){
clearInterval(this.interval);
}
}
ReactDOM.render(<App name="Mark" />, document.querySelector('#root'))
</script>
constructor
componentWillMount ⇒ getDerivedStateFromProps
render
componentDidMount
componentWillReceiveProps ⇒ getDerivedStateFromProps
shouldComponentUpdate
render
componentWillUpdate ⇒ getSnapshotBeforeUpdate
(dom 에 적용)
componentDidUpdate
componentWillUnmount
<script type="text/babel">
console.log(React);
console.log(ReactDOM);
let i =0;
class App extends React.Component {
state = {list: []};
render() {
return(
<div id="list" style={{height: 100, overflow:"scroll"}}>
{this.state.list.map((i) => {
return <div>{i}</div>;
})}
</div>
);
}
componentDidMount(){
setInterval(()=>{
this.setState((state) => ({
list: [...state.list, i++]
}))
}, 1000)
}
getSnapshotBeforeUpdate(prevProps, prevState){
if(prevState.list.length === this.state.list.length) return null;
const list = document.querySelector('#list');
return list.scrollHeight - list.scrollTop;
}
componentDidUpdate(prevProps, prevState, snapshot){
console.log(snapshot);
if(snapshot === null) return;
const list = document.querySelector('#list');
list.scrollTop = list.scrollHeight - snapshot;
}
}
ReactDOM.render(<App name="Mark" />, document.querySelector('#root'))
</script>
componentDidCatch
class App extends React.Component {
state = {
hasError: false
};
render(){
if(this.state.hasError){
return <div>예상치 못한 에러가 발생했다.</div>;
}
return <WebService />;
}
componentDidCatch(error, info){
this.setState({jasError: true});
}
}
ReactDOM.render(<App name="Mark" />, document.querySelector('#root'))