๋ณธ๋ฌธ์œผ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

[๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ] 7์žฅ - ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ

2020-04-05-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -7์žฅ-๋ผ์ดํ”„์‚ฌ์ดํด-๋ฉ”์„œ๋“œ-image-0

์ด ๊ธ€์€ ๊น€๋ฏผ์ค€(velopert)๋‹˜์˜ ๋ฆฌ์•กํŠธ๋ฅผ ๋‹ค๋ฃจ๋Š” ๊ธฐ์ˆ ์„์ฐธ์กฐํ•˜์—ฌ ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

๋ชจ๋“  ๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—๋Š” ๋ผ์ดํ”„์‚ฌ์ดํด(์ˆ˜๋ช… ์ฃผ๊ธฐ)์ด ์กด์žฌํ•œ๋‹ค. ๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด, ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฒ˜์Œ์œผ๋กœ ๋ Œ๋”๋งํ•  ๋•Œ ํ˜น์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ์ „ํ›„๋กœ ์–ด๋–ค ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•ด์•ผํ•  ์ˆ˜๋„ ์žˆ๊ณ , ๋ถˆํ•„์š”ํ•œ ์—…๋ฐ์ดํŠธ๋ฅผ ๋ฐฉ์ง€ํ•ด์•ผ ํ•  ์ˆ˜๋„์žˆ๋‹ค. ์ด๋•Œ ์ปดํฌ๋„ŒํŠธ์˜ ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ์˜ ์ดํ•ด#

๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋Š” ์ด ์•„ํ™‰ ๊ฐ€์ง€์ด๋‹ค.

Will ์ ‘๋‘์‚ฌ - ์–ด๋–ค ์ž‘์—…์„ ์ž‘๋™ํ•˜๊ธฐ ์ „์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ

Did ์ ‘๋‘์‚ฌ - ์–ด๋–ค ์ž‘์—…์„ ์ž‘๋™ํ•œ ํ›„์— ์‹คํ–‰๋˜๋Š” ๋ฉ”์„œ๋“œ

์ด ๋ฉ”์„œ๋“œ๋“ค์„ ์ปดํฌ๋„ŒํŠธ ํด๋ž˜์Šค์—์„œ ๋ฎ์–ด ์จ ์„ ์–ธํ•จ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ผ์ดํ”„์‚ฌ์ดํด์€ ์ด ์„ธ ๊ฐ€์ง€ ๋งˆ์šดํŠธ, ์—…๋ฐ์ดํŠธ, ์–ธ๋งˆ์šดํŠธ๋กœ ๋‚˜๋ˆˆ๋‹ค.

  1. ๋งˆ์šดํŠธ : ํŽ˜์ด์ง€์— ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋‚˜ํƒ€๋‚จ

  2. ์—…๋ฐ์ดํŠธ : ์ปดํฌ๋„ŒํŠธ ์ •๋ณด๋ฅผ ์—…๋ฐ์ดํŠธ(๋ฆฌ๋ Œ๋”๋ง)

  3. ์–ธ๋งˆ์šดํŠธ : ํŽ˜์ด์ง€์—์„œ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์‚ฌ๋ผ์ง


๋งˆ์šดํŠธ#

DOM์ด ์ƒ์„ฑ๋˜๊ณ  ์›น ๋ธŒ๋ผ์šฐ์ €์ƒ์— ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋งˆ์šดํŠธ(mount)๋ผ๊ณ  ํ•œ๋‹ค. ์ด ๋•Œ ๋‹ค์Œ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

์ปดํฌ๋„ŒํŠธ ๋งŒ๋“ค๊ธฐ

  1. constructor ๋ฉ”์„œ๋“œ : ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ค ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํด๋ž˜์Šค ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ

  2. getDerivedStateFromProps ๋ฉ”์„œ๋“œ : props ์— ์žˆ๋Š” ๊ฐ’์„ state ์— ๋„ฃ์„ ๋•Œ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ

  3. render ๋ฉ”์„œ๋“œ : ์ค€๋น„ํ•œ UI๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๋ฉ”์„œ๋“œ

  4. componentDidMount ๋ฉ”์„œ๋“œ : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €์ƒ์— ๋‚˜ํƒ€๋‚œ ํ›„์— ํ˜ธ์ถœํ•˜๋Š”๋ฉ”์„œ๋“œ


์—…๋ฐ์ดํŠธ#

์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์Œ์˜ ๊ฒฝ์šฐ์— ์—…๋ฐ์ดํŠธํ•œ๋‹ค.

  • props ๊ฐ€ ๋ฐ”๋€” ๋•Œ

  • state ๊ฐ€ ๋ฐ”๋€” ๋•Œ

  • ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง๋  ๋•Œ

  • this.forceUpdate ๋กœ ๊ฐ•์ œ๋กœ ๋ Œ๋”๋ง์„ ํŠธ๋ฆฌ๊ฑฐํ•  ๋•Œ


์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์—…๋ฐ์ดํŠธํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ

  1. getDerivedStateFromProps ๋ฉ”์„œ๋“œ : props ์— ์žˆ๋Š” ๊ฐ’์„ state ์— ๋„ฃ์„ ๋•Œ์‚ฌ์šฉํ•˜๋Š” ๋ฉ”์„œ๋“œ (๋งˆ์šดํŠธ์—์„œ๋„ ํ˜ธ์ถœ๋จ)

  2. shouldComponentUpdate ๋ฉ”์„œ๋“œ : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฆฌ๋ Œ๋”๋ง์„ ํ•ด์•ผ ํ• ์ง€ ๋ง์•„์•ผ ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ ( true ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ฆฌ๋ Œ๋”๋ง, false ๋ฉด ๋ฆฌ๋ Œ๋”๋ง ํ•˜์ง€ ์•Š์Œ.)

  3. render ๋ฉ”์„œ๋“œ : ์ค€๋น„ํ•œ UI๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๋ฉ”์„œ๋“œ

  4. getSnapshotBeforeUpdate : ์ปดํฌ๋„ŒํŠธ ๋ณ€ํ™”๋ฅผ DOM์— ๋ฐ˜์˜ํ•˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ

  5. componentDidUpdate : ์ปดํฌ๋„ŒํŠธ์˜ ์—…๋ฐ์ดํŠธ ์ž‘์—…์ด ๋๋‚œ ํ›„ ํ˜ธ์ถœํ•˜๋Š” ๋ฉ”์„œ๋“œ


์–ธ๋งˆ์šดํŠธ#

์ปดํฌ๋„ŒํŠธ๋ฅผ DOM์—์„œ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์„ ์–ธ๋งˆ์šดํŠธ(unmount)๋ผ๊ณ  ํ•œ๋‹ค. ์ปดํฌ๋„ŒํŠธ๋ฅผ ์–ธ๋งˆ์šดํŠธํ•˜๋ฉด ๋‹ค์Œ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

  • componentWIllUnmount : ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์›น ๋ธŒ๋ผ์šฐ์ €์ƒ์—์„œ ์‚ฌ๋ผ์ง€๊ธฐ ์ „์— ํ˜ธ์ถœํ•˜๋Š”๋ฉ”์„œ๋“œ

๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ์‚ดํŽด๋ณด๊ธฐ#

render#

์ปดํฌ๋„ŒํŠธ์˜ ๋ชจ์–‘์ƒˆ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ. ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ์ค‘ ์œ ์ผํ•œ ํ•„์ˆ˜ ๋ฉ”์„œ๋“œ์ด๋‹ค.

์ด ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ this.props ์™€ this.state ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด ๋ฉ”์„œ๋“œ ์•ˆ์—์„œ ์ด๋ฒคํŠธ ์„ค์ •์ด ์•„๋‹Œ ๊ณณ์—์„œ setState ๋ฅผ ์‚ฌ์šฉํ•ด์„  ์•ˆ๋˜๊ณ , ๋ธŒ๋ผ์šฐ์ €์˜ DOM์— ์ ‘๊ทผํ•ด์„œ๋„ ์•ˆ๋œ๋‹ค. DOM ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๊ฑฐ๋‚˜ state ์— ๋ณ€ํ™”๋ฅผ ์ค„ ๋•Œ๋Š” componentDidMount ์—์„œ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•œ๋‹ค.


constructor#

์ปดํฌ๋„ŒํŠธ์˜ ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค ๋•Œ ์ฒ˜์Œ์œผ๋กœ ์‹คํ–‰๋œ๋‹ค. ์ดˆ๊ธฐ state ๋ฅผ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.


getDerivedStateFromProps#

props ๋กœ ๋ฐ›์•„ ์˜จ ๊ฐ’์„ state ์— ๋™๊ธฐํ™”์‹œํ‚ค๋Š” ์šฉ๋„.

static getDerivedStateFromProps(nextProps, prevState) {  if(nextProps.value !== prevState.value) {    return { value: nextProps.value };  }  return null;}

componentDidMount#

์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ์ฒซ ๋ Œ๋”๋ง์„ ๋งˆ์นœ ํ›„ ์‹คํ–‰ํ•œ๋‹ค. ์ด ์•ˆ์—์„œ ๋‹ค๋ฅธ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋˜๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ ์ด๋ฒคํŠธ ๋“ฑ๋ก, setTimeout , setInterval , ๋„คํŠธ์›Œํฌ ์š”์ฒญ ๊ฐ™์€ ๋น„๋™๊ธฐ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.


shouldComponentUpdate#

props ๋˜๋Š” state ๋ฅผ ๋ณ€๊ฒฝํ–ˆ์„ ๋•Œ ๋ฆฌ๋ Œ๋”๋ง์„ ์‹œ์ž‘ํ• ์ง€ ์—ฌ๋ถ€๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ. ์ด ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ true ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์„œ ์–ธ์ œ๋‚˜ ๋ฆฌ๋ Œ๋”๋ง์„ํ•œ๋‹ค. false ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์—…๋ฐ์ดํŠธ ๊ณผ์ •์ด ์—ฌ๊ธฐ์„œ ์ค‘์ง€๋œ๋‹ค. (๋ฆฌ๋ Œ๋”๋ง๋˜์ง€ ์•Š๋Š”๋‹ค .)


getSnapshotBeforeUpdate#

render ์—์„œ ๋งŒ๋“ค์–ด์ง„ ๊ฒฐ๊ณผ๋ฌผ์ด ๋ธŒ๋ผ์šฐ์ €์— ์‹ค์ œ๋กœ ๋ฐ˜์˜๋˜๊ธฐ ์ง์ „์— ํ˜ธ์ถœ. ์ฃผ๋กœ ์—…๋ฐ์ดํŠธ ์ง์ „์˜ ๊ฐ’์„ ์ฐธ๊ณ ํ•  ์ผ์ด ์žˆ์„ ๋•Œ ํ™œ์šฉ๋œ๋‹ค.

getSnapshotBeforeUpdate(prevProps, prevState) {  if(prevState.array !== this.state.array) {    const { scrollTop, scrollHeight } = this.list    return { scrollTop, scrollHeight };  }}

componentDidUpdate#

componentDidUpdate(prevProps, prevState, snapshot){...}

๋ฆฌ๋ Œ๋”๋ง์„ ์™„๋ฃŒํ•œ ํ›„ ์‹คํ–‰ํ•œ๋‹ค. prevProps ๋˜๋Š” prevState ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด์ „์— ๊ฐ€์กŒ๋˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ getSnapshotBeforeUpdate ์—์„œ ๋ฐ˜ํ™˜ํ•œ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ snapshot ๊ฐ’์„ ์ „๋‹ฌ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.


componentWillUnmount#

์ปดํฌ๋„ŒํŠธ๋ฅผ DOM์—์„œ ์ œ๊ฑฐํ•  ๋•Œ ์‹คํ–‰. componentDidMount ์—์„œ ๋“ฑ๋กํ•œ ์ด๋ฒคํŠธ, ํƒ€์ด๋จธ, ์ง์ ‘ ์ƒ์„ฑํ•œ DOM์ด ์žˆ๋‹ค๋ฉด ์—ฌ๊ธฐ์„œ ์ œ๊ฑฐ ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค.


componentDidCatch#

์ปดํฌ๋„ŒํŠธ ๋ Œ๋”๋ง ๋„์ค‘์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋จนํ†ต์ด ๋˜์ง€ ์•Š๊ณ  ์˜ค๋ฅ˜ UI๋ฅผ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.

componentDidCatch(error, info) {  this.setState({    error: true  });  console.log({ error, info });}

error ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ด๋–ค ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€ ์•Œ๋ ค์ฃผ๊ณ  info ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์–ด๋””์— ์žˆ๋Š”์ฝ”๋“œ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ๋Š”์ง€์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ์ค€๋‹ค.


๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ#

LifeCycleSample.js ๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.

import React, {Component} from 'react';
class LifeCycleSample extends Component {    state = {        number: 0,        color: null,    };
    myRef = null;
    constructor(props) {        super(props);        console.log('constructor');    }
    static getDerivedStateFromProps(nextProps, prevState) {        console.log('getDerivedStateFromProps');        if (nextProps.color !== prevState.color) {            return {color: nextProps.color};        }        return null;    }
    componentDidMount() {        console.log('componentDidMount');    }
    shouldComponentUpdate(nextProps, nextState) {        console.log('shouldComponentUpdate', nextProps, nextState);        return nextState.number % 10 !== 4;    }
    componentWillUnmount() {        console.log('componentWillUnmount');    }
    handleClick = () => {        this.setState({            number: this.state.number + 1,        });    };
    getSnapshotBeforeUpdate(prevProps, prevState) {        console.log('getSnapshotBeforeUpdate');        if (prevProps.color !== this.props.color) {            return this.myRef.style.color;        }        return null;    }
    componentDidUpdate(prevProps, prevState, snapshot) {        console.log('componentDidUpdate', prevProps, prevState);        if (snapshot) {            console.log('์—…๋ฐ์ดํŠธ ๋˜๊ธฐ ์ง์ „ ์ƒ‰์ƒ: ', snapshot);        }    }
    render() {        console.log('render');
        const style = {            color: this.props.color,        };
        return (            <div>                <h1 style={style} ref={(ref) => (this.myRef = ref)}>                    {this.state.number}                </h1>                <p>color:{this.state.color}</p>                <button onClick={this.handleClick}>๋”ํ•˜๊ธฐ</button>            </div>        );    }}
export default LifeCycleSample;

์ด ์ปดํฌ๋„ŒํŠธ๋Š” ๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ์ฝ˜์†” ๋””๋ฒ„๊ฑฐ์— ๊ธฐ๋กํ•˜๊ณ  ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ props ๋กœ ์ƒ‰์ƒ์„ ๋ฐ›๊ณ , ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด state.number ๊ฐ’์„ 1์”ฉ ๋”ํ•œ๋‹ค.

shouldComponentUpdate ๋ฉ”์„œ๋“œ์—์„œ state.number ๊ฐ’์˜ ๋งˆ์ง€๋ง‰ ์ž๋ฆฌ ์ˆ˜๊ฐ€ 4์ด๋ฉด ๋ฆฌ๋ Œ๋”๋ง์„ ์ทจ์†Œํ•˜๋„๋ก ์„ค์ •ํ–ˆ๋‹ค.


App.js

import React, {Component} from 'react';import LifeCycleSample from './LifeCycleSample';
function getRandomColor() {    return '#' + Math.floor(Math.random() * 16777215).toString(16);}class App extends Component {    state = {        color: '#000000',    };    handleClick = () => {        this.setState({            color: getRandomColor(),        });    };    render() {        return (            <div>                <button onClick={this.handleClick}>๋žœ๋ค ์ƒ‰์ƒ</button>                <LifeCycleSample color={this.state.color} />            </div>        );    }}export default App;

2020-04-05-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -7์žฅ-๋ผ์ดํ”„์‚ฌ์ดํด-๋ฉ”์„œ๋“œ-image-1

์ฝ”๋“œ๋ฅผ ์ €์žฅํ•˜๋ฉด ๋‹ค์Œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.

์œ„์ชฝ ๋ฒ„ํŠผ๊ณผ ์•„๋ž˜์ชฝ ๋ฒ„ํŠผ์„ ๋ฒˆ๊ฐˆ์•„ ๋ˆ„๋ฅด๋ฉด ์•„๋ž˜์ฒ˜๋Ÿผ ๋‚˜ํƒ€๋‚œ๋‹ค.


์—๋Ÿฌ ์žก์•„๋‚ด๊ธฐ#

render ํ•จ์ˆ˜์—์„œ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ฑฐ๋‚˜, ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฐ์ฒด์˜๊ฐ’์„ ์กฐํšŒํ•  ๋•Œ ๋ฐœ์ƒํ•œ๋‹ค.

LifeCycleSample.js

render() {  console.log('render');
  const style = {    color: this.props.color,  };
  return (    <div>      {this.props.missing.value}      <h1 style={style} ref={(ref) => (this.myRef = ref)}>        {this.state.number}      </h1>      <p>color:{this.state.color}</p>      <button onClick={this.handleClick}>๋”ํ•˜๊ธฐ</button>    </div>  );}

์ด๋ ‡๊ฒŒ ์กด์žฌํ•˜์ง€ ์•Š๋Š” props ์ธ missing ๊ฐ์ฒด์˜ value ๋ฅผ ์กฐํšŒํ•ด์„œ ๋ Œ๋”๋งํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.


2020-04-05-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -7์žฅ-๋ผ์ดํ”„์‚ฌ์ดํด-๋ฉ”์„œ๋“œ-image-2


์˜ค๋ฅธ์ชฝ ์œ„์˜ X ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์˜ค๋ฅ˜ ์ฐฝ์ด ๋‹ซํžˆ๊ณ  ๋ธŒ๋ผ์šฐ์ €์—๋Š” ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ํฐ ํŽ˜์ด์ง€๋งŒ ๋‚จ๋Š”๋‹ค. ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ๋”ฐ๋กœ ์ฒ˜๋ฆฌํ•ด์ฃผ์ง€ ์•Š์œผ๋ฉด ์ด๋ ‡๊ฒŒ ๋นˆ ํŽ˜์ด์ง€๋งŒ ๋‚จ์•„์‚ฌ์šฉ์ž๋ฅผ ๋‹นํ™ฉ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ErrorBoundary ๋ผ๋Š” ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ƒ์„ฑํ•ด๋ณธ๋‹ค.

ErrorBoundary.js

import React, {Component} from 'react';
class ErrorBoundary extends Component {    state = {        error: false,    };
    componentDidCatch(error, info) {        this.setState({            error: true,        });        console.log({error, info});    }    render() {        if (this.state.error) return <div>์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!</div>;        return this.props.children;    }}
export default ErrorBoundary;

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด componentDidCatch ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉฐ, this.state.error ๊ฐ’์„ true ๋กœ ์—…๋ฐ์ดํŠธํ•ด์ค€๋‹ค. ๊ทธ๋Ÿฌ๋ฉด render ์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Œ์„ ์•Œ๋ ค์ฃผ๋Š” ๋ฌธ๊ตฌ๋ฅผ๋ณด์—ฌ์ค€๋‹ค.

์ด ์ปดํฌ๋„ŒํŠธ๋ฅผ App.js ์—์„œ LifeCycleSample ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ฐ์‹ธ์ค€๋‹ค.

App.js

import React, {Component} from 'react';import LifeCycleSample from './LifeCycleSample';import ErrorBoundary from './ErrorBoundary';
function getRandomColor() {    return '#' + Math.floor(Math.random() * 16777215).toString(16);}class App extends Component {    state = {        color: '#000000',    };    handleClick = () => {        this.setState({            color: getRandomColor(),        });    };    render() {        return (            <div>                <button onClick={this.handleClick}>๋žœ๋ค ์ƒ‰์ƒ</button>                <ErrorBoundary>                    <LifeCycleSample color={this.state.color} />                </ErrorBoundary>            </div>        );    }}export default App;

์—ฌ์ „ํžˆ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€๋งŒ, X ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ๋’ค ๋ธŒ๋ผ์šฐ์ €๋ฅผ ๋ณด๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ฃผ๋Š” ํŽ˜์ด์ง€๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ •๋ฆฌ#

2020-04-05-๋ฆฌ์•กํŠธ๋ฅผ-๋‹ค๋ฃจ๋Š”-๊ธฐ์ˆ -7์žฅ-๋ผ์ดํ”„์‚ฌ์ดํด-๋ฉ”์„œ๋“œ-image-3

๋ผ์ดํ”„์‚ฌ์ดํด ๋ฉ”์„œ๋“œ๋Š” ์ปดํฌ๋„ŒํŠธ ์ƒํƒœ์— ๋ณ€ํ™”๊ฐ€ ์žˆ์„ ๋•Œ๋งˆ๋‹ค ์‹คํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค. ์ด๋ฉ”์„œ๋“œ๋“ค์€ ์„œ๋“œํŒŒํ‹ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ DOM์„ ์ง์ ‘ ๊ฑด๋“œ๋ ค์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์œ ์šฉํ•˜๋‹ค. ์ปดํฌ๋„ŒํŠธ ์—…๋ฐ์ดํŠธ์˜ ์„ฑ๋Šฅ์„ ๊ฐœ์„ ํ•  ๋•Œ๋Š” shouldComponentUpdate ๊ฐ€ ์ค‘์š”ํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค.

๊ฐ™์ด ์ฝ๊ธฐ#

State and Lifecycle - React

React.Component - React