์ฃผ์˜ํ•˜์„ธ์š”!

์ปดํฌ๋„ŒํŠธ๋ฅผ ํด๋ž˜์Šค ๋Œ€์‹  ํ•จ์ˆ˜๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋ฐฉ๋ฒ•.

PureComponent๋Š” Component์™€ ๋น„์Šทํ•˜์ง€๋งŒ ๊ฐ™์€ props์™€ state์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๋ Œ๋”๋งํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์—์„œ ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋ฅผ ๊ณ„์† ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ƒˆ๋กœ์šด ์ฝ”๋“œ์—์„œ๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์„ ์ถ”์ฒœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

class Greeting extends PureComponent {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

๋ ˆํผ๋Ÿฐ์Šค

PureComponent

๊ฐ™์€ props์™€ state์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ๋ Œ๋”๋งํ•˜์ง€ ์•Š์œผ๋ ค๋ฉด Component ๋Œ€์‹  PureComponent๋ฅผ extend ํ•ด์ฃผ์„ธ์š”.

import { PureComponent } from 'react';

class Greeting extends PureComponent {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

PureComponent๋Š” Component์˜ ๋ชจ๋“  API๋ฅผ ์ง€์›ํ•˜๋Š” Component์˜ ์„œ๋ธŒํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค. PureComponent๋ฅผ extend ํ•˜๋Š” ๊ฒƒ์€ ๋‹จ์ˆœํžˆ props์™€ state๋ฅผ ๋น„๊ตํ•˜๋Š” ์‚ฌ์šฉ์ž shouldComponentUpdate ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์‹œ ๋ณด๊ธฐ.


์‚ฌ์šฉ๋ฒ•

ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ถˆํ•„์š”ํ•œ ์žฌ ๋ Œ๋”๋ง ๊ฑด๋„ˆ๋›ฐ๊ธฐ

๋ฆฌ์•กํŠธ๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๋ถ€๋ชจ๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง ๋  ๋•Œ๋งˆ๋‹ค ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋„ ๋‹ค์‹œ ๋ Œ๋”๋ง ํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ PureComponent๋ฅผ extend ํ•˜์—ฌ ์ƒˆ props ๋ฐ state๊ฐ€ ์ด์ „ props ๋ฐ state์™€ ๊ฐ™๋‹ค๋ฉด ๋ถ€๋ชจ๊ฐ€ ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜๋”๋ผ๋„ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜์ง€ ์•Š๋„๋ก Class component๋ฅผ ์ตœ์ ํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

class Greeting extends PureComponent {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์—๋Š” ํ•ญ์ƒ pure rendering ๋กœ์ง์ด ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, props, state ๋ฐ context๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ ๊ฐ™์€ ์ถœ๋ ฅ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. PureComponent๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์ด ์š”๊ตฌ ์‚ฌํ•ญ์„ ์ค€์ˆ˜ํ•œ๋‹ค๊ณ  ๋ฆฌ์•กํŠธ์—๊ฒŒ ์•Œ๋ฆฌ๋ฏ€๋กœ props ๋ฐ state๊ฐ€ ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๋Š” ํ•œ React๋Š” ๋‹ค์‹œ ๋ Œ๋”๋งํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์‚ฌ์šฉ ์ค‘์ธ context๊ฐ€ ๋ณ€๊ฒฝ๋œ๋‹ค๋ฉด ์ปดํฌ๋„ŒํŠธ๋Š” ๋‹ค์‹œ ๋ Œ๋”๋ง ๋ฉ๋‹ˆ๋‹ค.

์ด ์˜ˆ์ œ์—์„œ Greeting ์ปดํฌ๋„ŒํŠธ๋Š” name์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜์ง€๋งŒ (props ์ค‘ ํ•˜๋‚˜์ด๊ธฐ ๋•Œ๋ฌธ์—) address๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ์—๋Š” ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค (Greeting์— prop์œผ๋กœ ์ „๋‹ฌ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—).

import { PureComponent, useState } from 'react';

class Greeting extends PureComponent {
  render() {
    console.log("Greeting was rendered at", new Date().toLocaleTimeString());
    return <h3>Hello{this.props.name && ', '}{this.props.name}!</h3>;
  }
}

export default function MyApp() {
  const [name, setName] = useState('');
  const [address, setAddress] = useState('');
  return (
    <>
      <label>
        Name{': '}
        <input value={name} onChange={e => setName(e.target.value)} />
      </label>
      <label>
        Address{': '}
        <input value={address} onChange={e => setAddress(e.target.value)} />
      </label>
      <Greeting name={name} />
    </>
  );
}

์ฃผ์˜ํ•˜์„ธ์š”!

์ปดํฌ๋„ŒํŠธ๋ฅผ ํด๋ž˜์Šค ๋Œ€์‹  ํ•จ์ˆ˜๋กœ ์ •์˜ํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ๋ฐฉ๋ฒ•.


๋Œ€์•ˆ

PureComponent ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋กœ ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜ ํ•˜๊ธฐ

์ƒˆ๋กœ์šด ์ฝ”๋“œ์—์„œ๋Š” ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ ๋Œ€์‹  ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค. PureComponent๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ธฐ์กด ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜๋Š” ๊ธฐ์กด ์ฝ”๋“œ ์ž…๋‹ˆ๋‹ค.

import { PureComponent, useState } from 'react';

class Greeting extends PureComponent {
  render() {
    console.log("Greeting was rendered at", new Date().toLocaleTimeString());
    return <h3>Hello{this.props.name && ', '}{this.props.name}!</h3>;
  }
}

export default function MyApp() {
  const [name, setName] = useState('');
  const [address, setAddress] = useState('');
  return (
    <>
      <label>
        Name{': '}
        <input value={name} onChange={e => setName(e.target.value)} />
      </label>
      <label>
        Address{': '}
        <input value={address} onChange={e => setAddress(e.target.value)} />
      </label>
      <Greeting name={name} />
    </>
  );
}

import { memo, useState } from 'react';

const Greeting = memo(function Greeting({ name }) {
  console.log("Greeting was rendered at", new Date().toLocaleTimeString());
  return <h3>Hello{name && ', '}{name}!</h3>;
});

export default function MyApp() {
  const [name, setName] = useState('');
  const [address, setAddress] = useState('');
  return (
    <>
      <label>
        Name{': '}
        <input value={name} onChange={e => setName(e.target.value)} />
      </label>
      <label>
        Address{': '}
        <input value={address} onChange={e => setAddress(e.target.value)} />
      </label>
      <Greeting name={name} />
    </>
  );
}

์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค!

PureComponent์™€ ๋‹ฌ๋ฆฌ memo๋Š” ์ƒˆ state์™€ ์ด์ „ state๋ฅผ ๋น„๊ตํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋™์ผํ•œ state๋กœ set ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ memo ์—†์ด๋„ ๋‹ค์‹œ ๋ Œ๋”๋ง ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.