LifeCycle

August 07, 2021 · 6 mins read label-icon React

LifeCycle

  • λ¦¬μ•‘νŠΈ μ»΄ν¬λ„ŒνŠΈκ°€ 마운트, μ—…λ°μ΄νŠΈ, μ–Έλ§ˆμš΄νŠΈ 될 λ•Œμ˜ κ³Όμ •
  • νŠΉμ • 과정이 λ°œμƒν•  λ•Œ ν˜ΈμΆœλ˜λŠ” λ©”μ„œλ“œλ₯Ό LifeCycle Method 라고 ν•œλ‹€.
  • 곡식 λ¬Έμ„œμ— μžˆλŠ” λ„ν‘œλ₯Ό 보면 ν•œλˆˆμ— 확인 ν•  수 μžˆλ‹€.

마운트

  • constructor
  • getDerivedStateFromProps
  • render
  • componentDidMount

constructor

  • μ»΄ν¬λ„ŒνŠΈμ˜ μƒμ„±μž
  • μ»΄ν¬λ„ŒνŠΈκ°€ λ§Œλ“€μ–΄μ§€λ©΄ κ°€μž₯ λ¨Όμ € μ‹€ν–‰λœλ‹€.
  • constructor μ•ˆμ— setState λ₯Ό ν˜ΈμΆœν•˜λ©΄ μ•ˆ λœλ‹€.
1
2
3
4
5
6
7
constructor(props){
  super(props);
  console.log('constructor') 

  console.log(props) // {name: "sw"}
  this.state = { count: 0, name : '[κΈ°λ³Έ nameκ°’]' };
}

getDerivedStateFromProps

  • props 둜 받은 값을 state 에 μΆ”κ°€ν•˜λŠ” 경우 μ‚¬μš©
  • static ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 호좜
  • return 을 톡해 state 에 μΆ”κ°€
1
2
3
4
5
6
7
8
9
10
11
12
  static getDerivedStateFromProps(props, state) {
    console.log("getDerivedStateFromProps");
    console.log(props, state); // {name: "sw"}, {count: 0, name: "[κΈ°λ³Έ nameκ°’]"}

    if(props.name !== state.name){
      return { name: props.name }; // (*)
    }
    return null; // 아무 일도 μΌμ–΄λ‚˜μ§€ μ•ŠμŒ
  }

  // (*) λ‹€μŒμ— μ‹€ν–‰λ˜λŠ” render λ©”μ„œλ“œμ—μ„œ 값이 λ³€κ²½λœ 것을 확인할 수 μžˆλ‹€. 
  // {name: "sw"}, {count: 0, name: "sw"}

render

  • μ»΄ν¬λ„ŒνŠΈλ₯Ό λ Œλ”λ§ν•˜λŠ” λ©”μ„œλ“œ
  • 클래슀 μ»΄ν¬λ„ŒνŠΈμ—μ„œ λ°˜λ“œμ‹œ κ΅¬ν˜„λ˜μ–΄μ•Ό ν•œλ‹€.
1
2
3
4
5
6
7
8
render(){
  console.log("render");
  console.log(this.props, this.state) // {name: "sw"}, {count: 0, name: "sw"}

  return(
    <div>μ•ˆλ…•?</div>
  )
}

componentDidMount

  • μ»΄ν¬λ„ŒνŠΈμ˜ 첫 번째 λ Œλ”λ§μ΄ μ™„λ£Œλ˜μ—ˆμ„ λ•Œ μ‹€ν–‰ (ν•œ 번만 싀행됨)
  • 주둜 DOM 을 μ°Έμ‘°ν•˜λŠ” μž‘μ—…μ΄λ‚˜ 비동기 톡신(ajax, fetch, axios)을 ν•˜λŠ” 경우 μ‚¬μš©ν•œλ‹€.
1
2
3
componentDidMount(){
  console.log("componentDidMount");
}

μ—…λ°μ΄νŠΈ

  • getDerivedStateFromProps
  • shouldComponentUpdate
  • render
  • getSnapshotBeforeUpdate
  • componentDidUpdate

shouldComponentUpdate

  • return 값이 true 인 κ²½μš°μ—λ§Œ λ¦¬λ Œλ”λ§μ΄ μ§„ν–‰λœλ‹€.
  • 주둜 μ΅œμ ν™”λ₯Ό ν•˜λŠ” 경우 μ‚¬μš©λœλ‹€.
1
2
3
4
5
6
shouldComponentUpdate(nextProps, nextState){
  console.log("shouldComponentUpdate");
  console.log(nextProps, nextState); // countλ₯Ό +1 ν•œ 경우 => {name: "sw"}, {count: 1, name: "sw"}
  
  return nextState.count <= 10; // count 값이 10 μ΄ν•˜μΈ κ²½μš°μ—λ§Œ render(), componentDidUpdate() 이 싀행됨
}

getSnapshotBeforeUpdate

  • return 값이 componentDidUpdate()의 3번째 인자둜 μ „λ‹¬λœλ‹€.
1
2
3
4
5
6
7
8
getSnapshotBeforeUpdate(prevProps, prevState) {
  console.log("getSnapshotBeforeUpdate");

  if(prevState.count >= 5){ // count 값이 6 이상이 되면 'μŠ€λƒ…μƒ·!' 을 λ°˜ν™˜ν•œλ‹€.
    return "μŠ€λƒ…μƒ·!";
  }
  return null;
}

componentDidUpdate

  • λ¦¬λ Œλ”λ§μ΄ 된 뒀에 λ§ˆμ§€λ§‰μœΌλ‘œ ν˜ΈμΆœλ˜λŠ” λ©”μ„œλ“œ
  • 3번째 인자둜 getSnapshotBeforeUpdate()μ—μ„œ return 된 값을 λ°›λŠ”λ‹€.
1
2
3
4
5
6
7
  componentDidUpdate(prevProps, prevState, snapshot) {
    console.log("componentDidUpdate");

    console.log(prevProps, prevState, snapshot);
    // count 값이 5 μ΄ν•˜μΈ 경우 => {name: "sw"}, {count: 0~4, name: "sw"}, undefined
    // count 값이 6 이상인 경우 => {name: "sw"}, {count: 5~n, name: "sw"}, "μŠ€λƒ…μƒ·!" 
  }

μ–Έλ§ˆμš΄νŠΈ

  • componentWillUnmount

componentWillUnmount

  • μ»΄ν¬λ„ŒνŠΈκ°€ 제거되기 전에 ν˜ΈμΆœλœλ‹€.
1
2
3
componentWillUnmount() {
  console.log("componentWillUnmount");
}

+였λ₯˜ 처리

getDerivedStateFromError

  • ν•˜μœ„μ˜ μžμ‹ μ»΄ν¬λ„ŒνŠΈμ— μ—λŸ¬κ°€ λ°œμƒν•œ 경우 ν˜ΈμΆœλœλ‹€.
  • static ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 호좜
1
2
3
4
5
6
static getDerivedStateFromError(error) {
  console.log("componentWillUnmount");
  console.error(error);

  return { hasError: true };
}

componentDidCatch(error, info)

  • μžμ‹ μ»΄ν¬λ„ŒνŠΈμ—μ„œ 였λ₯˜κ°€ λ°œμƒν–ˆμ„ λ•Œ 호좜
  • β€˜μ»€λ°‹β€™ λ‹¨κ³„μ—μ„œ ν˜ΈμΆœλœλ‹€.
1
2
3
4
5
6
componentDidCatch(error, info) {
  console.error("componentDidCatch");

  console.log('error',error)
  console.log('info',info)
}

μ°Έκ³ 

https://ko.reactjs.org/docs/react-component.html
https://react.vlpt.us/basic/25-lifecycle.html