[React] PureComponent, React.memo, Ref

yeonddoriยท2023๋…„ 9์›” 18์ผ
0

AID_WEB

๋ชฉ๋ก ๋ณด๊ธฐ
8/10

๐Ÿ“ PureComponent

PureComponent๊ฐ€ Component์™€ ๋‹ค๋ฅธ ์ ์€ React์˜ ์ƒ๋ช… ์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ์ค‘ ํ•˜๋‚˜์ธ shouldComponentUpdate๋ฅผ ์–ด๋–ป๊ฒŒ ์“ฐ๋Š”๊ฐ€ ํ•˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

ํŠน์ง•

  • props๋ž‘ state๋ฅผ ์–•์€ ๋น„๊ต๋ฅผ ํ†ตํ•ด ๋น„๊ตํ•œ ๋’ค ๋ณ€๊ฒฝ๋œ ๊ฒƒ์ด ์žˆ์„๋•Œ๋Š” true๋ฅผ return ํ•ด์„œ ๋ฆฌ๋ Œ๋”๋ง ํ•˜๊ณ , ๋ณ€๊ฒฝ๋œ ๊ฒƒ์ด ์—†์„๋•Œ๋Š” false๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

์–•์€ ๋น„๊ต (shallow-compare)
1. ๋ณ€์ˆ˜์™€ ๋ฌธ์ž์—ด์—์„œ๋Š” ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.
2. ๊ฐ์ฒด์—์„œ๋Š” reference ๊ฐ’์„ ๋น„๊ตํ•œ๋‹ค.

Component์™€ ์ฐจ์ด์ 

Component๋Š” shouldComponentUpdate๋ฅผ ๋”ฐ๋กœ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์€ ๊ฒฝ์šฐ ํ•ญ์ƒ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ, setState๊ฐ€ ์‹คํ–‰๋˜๋ฉด state, props์˜ ๋ณ€๊ฒฝ ์—ฌ๋ถ€๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๊ณ  ๋ฌด์กฐ๊ฑด์ ์œผ๋กœ Component๋ฅผ ์—…๋ฐ์ดํŠธํ•œ๋‹ค.
ํ•˜์ง€๋งŒ PureComponent์˜ ๊ฒฝ์šฐ ํ˜„์žฌ state, props์™€ ๋ฐ”๋€” state, props๋ฅผ ๋น„๊ตํ•˜์—ฌ ์—…๋ฐ์ดํŠธ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๐Ÿ“ React.memo

React.memo๋Š” ๊ณ ์ฐจ ์ปดํฌ๋„ŒํŠธ(Higher Order Component)์ด๋‹ค.

ํŠน์ง•

  • useMemo์ฒ˜๋Ÿผ Memoization ๊ธฐ๋ฒ•์œผ๋กœ ๋™์ž‘ํ•จ.
  • props๊ฐ€ ์ด์ „๊ณผ ๋™์ผํ•œ ๊ฐ’์ด๋ฉด ์žฌ๋ Œ๋”๋งํ•˜์ง€ ์•Š๊ณ , ๋‹ค๋ฅธ ๊ฐ’์ด๋ฉด ์žฌ๋ Œ๋”๋งํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ค์–ด ๋ฐ˜ํ™˜ํ•จ.
  • React.memo์— ์“ฐ์ธ ์ปดํฌ๋„ŒํŠธ ์•ˆ์—์„œ ๊ตฌํ˜„ํ•œ state๊ฐ€ ๋ณ€๊ฒฝ๋˜๋ฉด ์ปดํฌ๋„ŒํŠธ๋Š” ์žฌ๋ Œ๋”๋ง์ด ๋จ.

props๊ฐ€ ๊ฐ–๋Š” ๋ณต์žกํ•œ ๊ฐ์ฒด์— ๋Œ€ํ•˜์—ฌ ์–•์€ ๋น„๊ต๋งŒ์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ ๋™์ž‘์ด๋‹ค. ๋‹ค๋ฅธ ๋น„๊ต ๋™์ž‘์„ ์›ํ•œ๋‹ค๋ฉด, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ณ„๋„์˜ ๋น„๊ต ํ•จ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋ฉด ๋œ๋‹ค.


๐Ÿ“ Ref

Ref๋Š” render ๋ฉ”์„œ๋“œ์—์„œ ์ƒ์„ฑ๋œ DOM ๋…ธ๋“œ๋‚˜ React ์—˜๋ฆฌ๋จผํŠธ์— ์ ‘๊ทผํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•œ๋‹ค.

์‚ฌ์šฉ์ฒ˜

  • ์ž์‹ ์ปดํผ๋„ŒํŠธ๋ฅผ ์ง์ ‘ ์ ‘๊ทผํ•˜์—ฌ ์ˆ˜์ •ํ•  ๋•Œ
  • DOM ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ ‘๊ทผํ•˜๊ณ  ์‹ถ์„ ๋•Œ

์˜ˆ์‹œ

  • ํฌ์ปค์Šค, ํ…์ŠคํŠธ ์„ ํƒ์˜์—ญ, ํ˜น์€ ๋ฏธ๋””์–ด์˜ ์žฌ์ƒ์„ ๊ด€๋ฆฌํ•  ๋•Œ
  • ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ง์ ‘์ ์œผ๋กœ ์‹คํ–‰์‹œํ‚ฌ ๋•Œ
  • third-party DOM ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ React์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๋•Œ

Ref ์ƒ์„ฑ

  1. createRef์„ ํ†ตํ•œ ref ์„ค์ •
    ์ปดํฌ๋„ŒํŠธ ๋‚ด๋ถ€์—์„œ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ React.createRef()๋ฅผ ๋‹ด๊ณ , ํ•ด๋‹น ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋ฅผ ref๋ฅผ ๋‹ฌ๊ณ ์ž ํ•˜๋Š” ์š”์†Œ์— ref props๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ref์„ค์ •์ด ์™„๋ฃŒ๋œ๋‹ค. ์„ค์ •ํ•œ ๋’ค ๋‚˜์ค‘์— ref๋ฅผ ์„ค์ •ํ•ด ์ค€ DOM์— ์ ‘๊ทผํ•˜๋ ค๋ฉด this.input.current๋ฅผ ์กฐํšŒํ•˜๋ฉด ๋œ๋‹ค.
    ์ด ๊ธฐ๋Šฅ์€ ๋ฆฌ์•กํŠธ v16.3๋ถ€ํ„ฐ ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ ์ด์ „ ๋ฒ„์ „์—์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š๋Š”๋‹ค.
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.myRef = React.createRef();
  }
  render() {
    return <div ref={this.myRef} />;
  }
}

  1. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ํ†ตํ•œ ref ์„ค์ •
    ref๋ฅผ ๋งŒ๋“œ๋Š” ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ๋ฒ•์€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ref๋ฅผ ๋‹ฌ๊ณ ์ž ํ•˜๋Š” ์š”์†Œ์— ref๋ผ๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋ฅผ props๋กœ ์ „๋‹ฌํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ด ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋Š” ref ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ ๋ฐ›์•„ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ref๋ฅผ ์ปดํฌ๋„ŒํŠธ์˜ ๋ฉค๋ฒ„ ๋ณ€์ˆ˜๋กœ ์„ค์Ÿํ•ด ์ค€๋‹ค.
class CustomTextInput extends React.Component {
  constructor(props) {
    super(props);

    this.textInput = null;

    this.setTextInputRef = element => {
      this.textInput = element;
    };

    this.focusTextInput = () => {
      // DOM API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ text ํƒ€์ž…์˜ input ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํฌ์ปค์Šคํ•œ๋‹ค.
      if (this.textInput) this.textInput.focus();
    };
  }

  componentDidMount() {
    // ๋งˆ์šดํŠธ ๋˜์—ˆ์„ ๋•Œ ์ž๋™์œผ๋กœ text ํƒ€์ž…์˜ input ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ํฌ์ปค์Šคํ•œ๋‹ค.
    this.focusTextInput();
  }

  render() {
    // text ํƒ€์ž…์˜ input ์—˜๋ฆฌ๋จผํŠธ์˜ ์ฐธ์กฐ๋ฅผ ์ธ์Šคํ„ด์Šค์˜ property
    // (์˜ˆ๋ฅผ ๋“ค์–ด`this.textInput`)์— ์ €์žฅํ•˜๊ธฐ ์œ„ํ•ด `ref` ์ฝœ๋ฐฑ์„ ์‚ฌ์šฉํ•œ๋‹ค.
    return (
      <div>
        <input
          type="text"
          ref={this.setTextInputRef}
        />
        <input
          type="button"
          value="Focus the text input"
          onClick={this.focusTextInput}
        />
      </div>
    );
  }
}

๐Ÿช„ ๋งˆ์น˜๋ฉด์„œ

shouldComponentUpdate๋ฅผ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š์•„๋„ ๋˜๋Š” ๊ฒƒ์€ PureComponent์™€ React.memo ๋‘˜ ๋‹ค ํ•ด๋‹น๋˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ, ์–ด๋–จ ๋•Œ PureComponent๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  React.memo๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ธ์ง€๋Š” ์•„์ง ์ž˜ ๋ชจ๋ฅด๊ฒ ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€