ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • React :: Component, Props, State
    PROGRAMMING/React 2021. 3. 20. 19:32

    Component

    ๊ฐœ๋…์ ์œผ๋กœJavaScript ํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•œ Component ๋Š”,

    • Props ๋ผ๋Š” ์ž…๋ ฅ์„ ๋ฐ›๊ณ ,
    • ๋กœ์ปฌ State ๋ฅผ ๊ฐ€์ง€๊ณ ,
    • React Element ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    ๋”ฐ๋ผ์„œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ†ตํ•ด ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๊ฐœ๋ณ„ '์กฐ๊ฐ'์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

    ๊ฐ€์žฅ ๊ฐ„๋‹จํžˆ, ์ด๋ฆ„์„ Props ๋กœ ๋ฐ›์•„์„œ "Hello, {user}" ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” Element ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” Component ๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•˜์ž.

    ๊ทธ๋Ÿฌ๋ฉด ๊ทธ Component ๋ฅผ ๋‹ค๋ฅธ Compnent ์—์„œ import ํ•ด์„œ ํ•œ ์กฐ๊ฐ์„ ๊ฐ–๋‹ค ๋ถ™์ด๋“ฏ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

     

    * Props ์˜ ๊ฐœ๋…์€ ๋ฐ‘์—์„œ ๋‹ค๋ฃจ๊ณ , ์ผ๋‹จ Component ๊ฐ€ ์™ธ๋ถ€์—์„œ ๋ฐ›์•„์˜ค๋Š” ๋ฐ์ดํ„ฐ ๋ผ๊ณ ๋งŒ ์•Œ๊ณ  ์žˆ์ž.

     

    ์ฃผ์˜ํ•  ์‚ฌํ•ญ์€, Component ์ด๋ฆ„์€ ํ•ญ์ƒ! ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•œ๋‹ค.

    React ๋Š” ์†Œ๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” DOM ํƒœ๊ทธ (div ๋“ฑ) ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

     

    ์ด๋Ÿฐ Component ๋ฅผ ์ •์˜ํ•˜๋ ค๋ฉด ํด๋ž˜์Šค/ํ•จ์ˆ˜ ๋ฅผ ํ†ตํ•ด ์ •์˜ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด์ž.

     

    ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ

    Component ๋ฅผ ์ •์˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ, JavaScript ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

    function Welcome(props) {
      return <h1>Hello, {props.name}</h1>;
    }

    ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ, JavaScript ํ•จ์ˆ˜์ด๋ฏ€๋กœ "ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ" ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

     

    ์ด๋Š” ํ˜„์žฌ CRA (Creat-React-App. 2021.03.19) ๋ฅผ ํ†ตํ•ด ์ƒˆ React ์•ฑ์„ ๋งŒ๋“ค์—ˆ์„ ๋•Œ

    ./my-app/src/App.js ์—์„œ ์‚ฌ์šฉํ•œ ์ปดํฌ๋„ŒํŠธ ์ •์˜ ๋ฐฉ์‹์ด๋‹ค.

    import logo from './logo.svg';
    import './App.css';
    
    function App() {
      return (
        <div className="App">
          <header className="App-header">
            <img src={logo} className="App-logo" alt="logo" />
            <p>
              Edit <code>src/App.js</code> and save to reload.
            </p>
            <a
              className="App-link"
              href="https://reactjs.org"
              target="_blank"
              rel="noopener noreferrer"
            >
              Learn React
            </a>
          </header>
        </div>
      );
    }
    
    export default App;

    ์ด๋Ÿฐ ์‹์œผ๋กœ App Component ๋ฅผ ๋งŒ๋“ค๊ณ , export ํ•˜์—ฌ ๋‹ค๋ฅธ ๊ณณ์—์„œ๋„ App Component ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์ด Component ์˜ ์‚ฌ์šฉ ๊ฐœ๋…์ด๋‹ค.

     

    ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ

    ES6 Class ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    class Welcome extends React.Component {
      render() {
        return <h1>Hello, {this.props.name}</h1>;
      }
    }

    ์ด๋Š” ์œ„์—์„œ ์ž‘์„ฑํ•œ Welcome ๊ณผ React ๊ด€์ ์—์„œ๋Š” ๋™์ผํ•˜๋‹ค!

    ๋‹ค๋งŒ ๋‘ ๋ฐฉ์‹์—์„œ ์„œ๋กœ ๋‹ค๋ฅธ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ๋“ค์ด ์กด์žฌํ•œ๋‹ค.

     

    ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•˜๋ฉด ๊ฐ„๊ฒฐํ•˜๋‹ค๋Š” ์žฅ์ ์ด,

    ํด๋ž˜์Šค ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•˜๋ฉด ์—ฌ๋Ÿฌ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์กด์žฌํ•œ๋‹ค.

     

    ์ปดํฌ๋„ŒํŠธ ํ•ฉ์„ฑ

    ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์˜ ์ถœ๋ ฅ์— ๋‹ค๋ฅธ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

    function Friends() {
      return (
        <div>
          <Welcome name="React" />
          <Welcome name="HTML" />
          <Welcome name="JavaScript" />
        </div>
      );
    }

     


    Props

    ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ์ž‘์„ฑํ•œ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์žˆ์œผ๋ฉด,

    JSX ์–ดํŠธ๋ฆฌ๋ทฐํŠธ์™€ ์ง€์‹์„ ํ•ด๋‹น ์ปดํฌ๋„ŒํŠธ์— ๋‹จ์ผ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•˜๋Š”๋ฐ,

    ๊ทธ ๊ฐ์ฒด๊ฐ€ ๋ฐ”๋กœ "Props" ์ด๋‹ค.

     

    ์ฆ‰, ์ง€๊ธˆ๊นŒ์ง€๋Š” Element ๋ฅผ DOM ํƒœ๊ทธ(div,,๋“ฑ) ์œผ๋กœ ๋‚˜ํƒ€๋ƒˆ๋Š”๋ฐ, ์ด์ œ๋Š” ์‚ฌ์šฉ์ž ์ •์˜ ์ปดํฌ๋„ŒํŠธ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉด์„œ, ๊ทธ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ porps ์ด๋ผ๋Š” ํ˜•ํƒœ๋กœ ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ด๋‹ค.

     

    ์•ž์„œ์„œ ๋งŒ๋“ค์—ˆ๋˜ Welcome ์ปดํฌ๋„ŒํŠธ๋กœ Element ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

    const element = <Welcome name="React" />;

    ๊ทธ๋Ÿผ Welcome ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์˜ํ•˜๊ณ , ๋ Œ๋”๋งํ•˜๋Š” ๊ณผ์ •์„ ์‚ดํŽด๋ณด์ž.

    function Welcome(props) {
      return <h1>Hello, {props.name}</h1>;
    }
    
    const element = <Welcome name="React" />;
    ReactDOM.render(
      element,
      document.getElementById('root')
    );
    1. <Welcome name="React" /> ์—˜๋ฆฌ๋จผํŠธ๋กœ ReactDOM.render() ํ˜ธ์ถœ
    2. React ๊ฐ€ ์ด element ๋ฅผ ๋ฐœ๊ฒฌํ•˜๋ฉด, {name: 'React'} ๋ฅผ props ๋กœ ํ•˜์—ฌ Welcome ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    3. Welcome ์ปดํฌ๋„ŒํŠธ๋Š” <h1>Hello, React</h1> ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    4. ReactDOM ์€ <h1>Hello, React</h1> ์—˜๋ฆฌ๋จผํŠธ์™€ ์ผ์น˜ํ•˜๋„๋ก DOM ์„ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค. (๋ณ€๊ฒฝ์‚ฌํ•ญ๋งŒ, ํšจ์œจ์ ์œผ๋กœ.)

    Props ์˜ ์ด๋ฆ„์€ ์‚ฌ์šฉ๋  Context ๊ฐ€ ์•„๋‹Œ ์ปดํฌ๋„ŒํŠธ ์ž์ฒด์˜ ๊ด€์ ์—์„œ ์ง“๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค.

    ex. author ๋ณด๋‹ค user ์™€ ๊ฐ™์ด ๋” ์ผ๋ฐ˜ํ™”๋œ ํ˜•ํƒœ๋ฅผ ์‚ฌ์šฉ. (์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋‹ฌ๋ผ์ง€๋‹ˆ, ํ•œ ๊ฐ€์ง€ ์˜ˆ์‹œ์ผ ๋ฟ!)

     

    ์ฃผ์˜ ์‚ฌํ•ญ

    • Props ๋Š” ์ฝ๊ธฐ ์ „์šฉ์ด๋‹ค
    • ๋ชจ๋“  Component ๋Š” ์ž์‹ ์˜ Props ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์ˆœ์ˆ˜ ํ•จ์ˆ˜์ฒ˜๋Ÿผ ๋™์ž‘ํ•ด์•ผ ํ•œ๋‹ค.

    ์ˆœ์ˆ˜ ํ•จ์ˆ˜๋ž€, ์–ด๋–ค ๊ฐ’์ด ๋“ค์–ด์™€๋„ ํ•ญ์ƒ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

    ์ฆ‰,  Component ๋Š” ์–ด๋–ค ์ƒํ™ฉ์—์„œ๋“  Porps ๋ฅผ ์ˆ˜์ •ํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค!

     

    ์ด๊ฒƒ์€ ์œ ์—ฐํ•œ React ์—๊ฒŒ ์กด์žฌํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ์—„๊ฒฉํ•œ ๊ทœ์น™์ด๋‹ค.

     

    Default Props

    Props ๋˜ํ•œ Default ๊ฐ’์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

    (๋งˆ์น˜ ํ•จ์ˆ˜์—์„œ ๋””ํดํŠธ ์ธ์ž๊ฐ’์„ ์ง€์ •ํ•ด ์ฃผ๋“ฏ..)

    class Welcome extends React.Component {
      static defaultProps = {
        name: "React"
      }
        
      render() {
    	return <h1>Hello, {this.props.name}</h1>;
      }
    }

     

    Props ์™€ ํ•จ์ˆ˜ Component

    ๋‹จ์ˆœํžˆ Props ๋ฅผ ๊ฐ€์ ธ์™€์„œ ๋ณด์—ฌ์ฃผ๊ธฐ๋งŒ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ๋Š” ํ•จ์ˆ˜ ์ปดํฌ๋„ŒํŠธ๋กœ ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

    ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ, ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

    const Welcome = ({name}) => {
      return (
        <div>
          Hello, {name}
        </div>
      );
    );

    State

    • ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ–๋Š” ๊ฐ์ฒด๋ผ๋Š” ์ ์—์„œ props ์™€ ์œ ์‚ฌํ•˜์ง€๋งŒ
    • State ๋Š” ๋น„๊ณต๊ฐœ์ด๋ฉฐ,
    • ์ปดํฌ๋„ŒํŠธ์— ์˜ํ•ด ์™„์ „ํžˆ ์ œ์–ด๋œ๋‹ค.

    state ๊ฐ€ ์†Œ์œ ํ•˜๊ณ  ์„ค์ •ํ•œ Component ์™ธ์—๋Š” ์–ด๋– ํ•œ Component ์—๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋‹ค.

    ์ด ๋•Œ๋ฌธ์— state ๋Š” ์ข…์ข… ๋กœ์ปฌ ๋˜๋Š” ์บก์Šํ™”๋ผ๊ณ  ๋ถˆ๋ฆฐ๋‹ค.

     

    ๊ทธ๋ ‡๊ธฐ์— Component ๋Š” ์ž์‹ ์˜ state ๋ฅผ ์ž์‹ Component ์— props ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,

    Component ๋Š” ์ž์‹ ์ด ๋ฐ›์€ props ์ด ์ƒ์œ„ Component ์—์„œ state ์˜€๋Š”์ง€, props ์˜€๋Š”์ง€ ๋˜๋Š” ์ˆ˜๋™์œผ๋กœ ์ž…๋ ฅํ•œ ๊ฒƒ์ธ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค.

     

    ์ด๋ ‡๊ฒŒ ์œ„์—์„œ ์•„๋ž˜๋กœ (๋ถ€๋ชจ-์ž์‹) ํ๋ฅด๋Š” ๋ฐ์ดํ„ฐ ํ๋ฆ„์„ "ํ•˜ํ–ฅ์‹(top-down)" ๋˜๋Š” "๋‹จ๋ฐฉํ–ฅ์‹" ๋ฐ์ดํ„ฐ ํ๋ฆ„์ด๋ผ๊ณ  ํ•œ๋‹ค.

     

    ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž.

    class Welcome extends React.Component {
      constructor(props) {
        super(props);
        this.state = {comment: "Hello"};
      }
      
      render() {
        return (
          <div>
            <h1>{this.state.comment}, {this.props.name}</h1>
          </div>
        );
      }
    }

     

    state ๋Š” class field ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    class Welcome extends React.Component {
      state = {
        comment: "Hello"
      }
      
      render() {
        return (
          <div>
            <h1>{this.state.comment}, {this.props.name}</h1>
          </div>
        );
      }
    }

    ์ด๋Š” ์œ„์™€ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚ธ๋‹ค.

     

    super(props)

    Component ๋ฅผ ๋งŒ๋“ค๋ฉด์„œ constructor ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด ๊ธฐ์กด ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ๋ฎ์–ด์“ฐ๊ฒŒ ๋œ๋‹ค.

    ๋”ฐ๋ผ์„œ ์ƒ์†ํ•œ Component ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” constructor ๋ฅผ super ๋กœ ์‹คํ–‰ํ•˜๊ณ , ๊ทธ ํ›„์— state ๋ฅผ ์„ค์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

     

    setState()

    ์ž‘์„ฑํ•œ state ๋Š” setState() ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์•„๋ž˜์— ์˜ˆ์‹œ์™€, ์ฃผ์˜ ์‚ฌํ•ญ์„ ์ž˜ ๋ด๋ณด์ž.

     

    ๋ณ‘ํ•ฉ

    setState() ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ React๋Š” ์ œ๊ณตํ•œ ๊ฐ์ฒด๋ฅผ ํ˜„์žฌ state๋กœ ๋ณ‘ํ•ฉํ•œ๋‹ค.

    ์ฆ‰ state ๋Š” ๋…๋ฆฝ์ ์ธ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, setState() ํ•จ์ˆ˜ ํ˜ธ์ถœ๋กœ ๊ฐ ๋ณ€์ˆ˜๋ฅผ ๋…๋ฆฝ์ ์œผ๋กœ ์—…๋ฐ์ดํŠธํ•  ์ˆ˜ ์žˆ๋‹ค.

     

    ๊ฐ„๋‹จํ•œ ์˜ˆ์‹œ๋ฅผ ์‚ดํŽด๋ณด์ž. (๋งŽ์ด ์ถ•์•ฝ๋œ ํ˜•ํƒœ)

    ...
    constructor(props){
      super(props);
      this.state = {
        name: [],
        comment: []
      };
    }
    ...
    
    ...
    this.setState({comment: "Changed!"});
    ...

    this.setState({comment}) ๋Š” this.state.name ์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์ง€๋งŒ, this.state.comment ๋Š” ๋Œ€์ฒด๋œ๋‹ค!

     

     

    ์ฃผ์˜์‚ฌํ•ญ

    • state ๋Š” ์ง์ ‘ ์ˆ˜์ •ํ•˜์ง€ ๋ง๊ณ , setState() ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค
    //Wrong
    this.state.comment = 'Hello';
    //Correct
    this.setState({comment: 'Hello'});

    this.state ๋ฅผ ์ง์ ‘ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์œ ์ผํ•œ ๊ณต๊ฐ„์€ constructor ์ด๋‹ค.

     

    • State ์—…๋ฐ์ดํŠธ๋Š” ๋น„๋™๊ธฐ์ ์ผ ์ˆ˜ ์žˆ์œผ๋‹ˆ, state ๋ฅผ ๋ณ€๊ฒฝํ•  ๋•Œ ํ•ด๋‹น ๊ฐ’์— ์˜์กดํ•ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
    // Wrong
    this.setState({
      counter: this.state.counter + this.props.increment,
    });
    
    // Correct
    this.setState((state, props) => ({
      counter: state.counter + props.increment
    }));

    Wrong ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด ์„ฑ๊ณต์ ์œผ๋กœ counter ๊ฐ’์ด ์—…๋ฐ์ดํŠธ ๋  ์ˆ˜ ์žˆ์ง€๋งŒ,

    ๋น„๋™๊ธฐ์ ์œผ๋กœ state ๋ฅผ ์—…๋ฐ์ดํŠธ ํ•œ๋‹ค๋ฉด, ์ œ๋Œ€๋กœ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๊ฒƒ์„ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.

     


    ์ด์ƒ์œผ๋กœ React ์˜ Component , Props , State ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค.

    ๋‹ค์Œ์œผ๋กœ๋Š” ์ƒ๋ช…์ฃผ๊ธฐ์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

     


    Reference

    React ๊ณต์‹ ๋ฌธ์„œ

    ko.reactjs.org/docs/components-and-props.html

    ko.reactjs.org/docs/state-and-lifecycle.html

    .

    ๋Œ“๊ธ€

Designed by Tistory.