๐Ÿ‘ฉโ€๐Ÿ’ป React JS - ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•œ ์ตœ๊ณ ์˜ ๊ฐ€์ด๋“œ! (2๋ถ€๐Ÿ”ฅ)

27113 ๋‹จ์–ด reactcareerbeginnersjavascript

๊ตฌ์„ฑ ์š”์†Œ ์—ฐ๊ฒฐ



๋ฌผ๋ก  ๊ตฌ์„ฑ ์š”์†Œ ๊ธฐ๋ฐ˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ๋Š” ํ•˜๋‚˜์˜ ๊ตฌ์„ฑ ์š”์†Œ๋งŒ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์—ฌ๋Ÿฌ ๊ฐœ๋ฅผ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ์—์„œ๋Š” ํŽ˜์ด์ง€์— ์ฃผ์ž…๋  ํ•˜๋‚˜์˜ ๊ตฌ์„ฑ ์š”์†Œ๋งŒ ์ œ๊ณตํ•˜์ง€๋งŒ ๊ทธ ์•ˆ์—๋Š” ์ˆ˜์‹ญ, ์ˆ˜๋ฐฑ ๊ฐœ์˜ ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ํ•œ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์€ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ•˜๋‚˜์˜ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. , ์ด๊ฒƒ์€ ๋‹ค๋ฅธ ์—ฌ๋Ÿฌ ์ž๋…€๊ฐ€ โ€‹โ€‹์žˆ์Šต๋‹ˆ๋‹ค.

๊ณ„์†ํ•˜๋ ค๋ฉด ํ•œ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ดํ•ดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์˜ˆ์ œ๋กœ ์ด๋™ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ง€๋‚œ ๊ธฐ์‚ฌ์—์„œ ๋งŒ๋“  index.html ํŒŒ์ผ์—์„œ ํŽธ์ง‘๊ธฐ๋ฅผ ์—ฝ๋‹ˆ๋‹ค.

function MyComponente() {
        return React.createElement("div", null, "Hello world");
      }


์ด๊ฒƒ์ด ์šฐ๋ฆฌ์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด์ œ ์šฐ๋ฆฌ๋Š” ๋„ค ๊ฐœ์˜ ๊ตฌ์„ฑ ์š”์†Œ๋ฅผ ๋” ๋งŒ๋“ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ ์ค‘ ๋‹ค์„ฏ ๋ฒˆ์งธ๋Š” ์ฒซ ๋ฒˆ์งธ MyComponent ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

      function MyComponent1(){

      }
      function MyComponent2(){

      }
      function MyComponent3(){

      }
      function MyComponent4(){

      }

      function MyComponent() {
        return React.createElement("div", null, "Hello world");
      }


์ด์ œ ์šฐ๋ฆฌ๋Š” ํ•œ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ์˜ ๋ถ€๋ชจ๊ฐ€ ๋˜๊ธฐ๋ฅผ ์›ํ•ฉ๋‹ˆ๋‹ค.

function MyComponent1(){
        return React.createElement(MyComponent2)
      }
      function MyComponent2(){
        return React.createElement(MyComponent3)

      }
      function MyComponent3(){
        return React.createElement(MyComponent4)

      }
      function MyComponent4(){
        return React.createElement('p', null, 'Component 4')

      }

      function MyComponent() {
        return React.createElement("div", null, "Hello world");
      }


๊ธฐ๋ณธ์ ์œผ๋กœ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ์ž์‹์„ ํ˜ธ์ถœํ•˜๊ณ , MyComponent1์ด MyComponent2๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , MyComponent3๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , HTML ๋‹จ๋ฝ('p'), ์†์„ฑ ์—†์Œ(null), ์ž‘์„ฑ๋œ Component 4๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” MyComponent4๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ์ƒ์„ฑ๋œ ์ฒซ ๋ฒˆ์งธ ๊ตฌ์„ฑ ์š”์†Œ์ธ MyComponent๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด MyComponent1์„ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

function MyComponent() {
        return React.createElement("div", null, 
          React.createElement(MyComponent1)
        );
      }


์ด ๊ฒฝ์šฐ ์šฐ๋ฆฌ์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ(MyComponent)๋Š” 1(MyComponent1)์„ ๋ Œ๋”๋งํ•˜๋Š” ์‹์œผ๋กœ ์„œ๋กœ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ณด๊ธฐ





p์˜ div ์•ˆ์— ์ปดํฌ๋„ŒํŠธ 4๊ฐ€ ๋ Œ๋”๋ง๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

<div id="app">
        <div>
            <p>Component 4</p>
        </div>
</div>


๊ทธ๋ƒฅ ํ˜ธ๊ธฐ์‹ฌ์— ๋ž˜ํผ์™€ ์ปจํ…Œ์ด๋„ˆ div๋ฅผ ๋„ฃ์–ด๋ด…์‹œ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ๊ฒผ์Šต๋‹ˆ๋‹ค.

function MyComponent1() {
        return React.createElement(
          "div",
          null,
          React.createElement(MyComponent2)
        );
      }
      function MyComponent2() {
        return React.createElement(
          "div",
          null,
          React.createElement(MyComponent3)
        );
      }
      function MyComponent3() {
        return React.createElement(
          "div",
          null,
          React.createElement(MyComponent4)
        );
      }
      function MyComponent4() {
        return React.createElement("p", null, "Component 4");
      }

      function MyComponent() {
        return React.createElement(
          "div",
          null,
          React.createElement(MyComponent1)
        );
      }

      ReactDOM.render(
        React.createElement(MyComponent),
        document.getElementById("app")
      );


์ด์ œ ๋ณ€๊ฒฝ๋œ ์‚ฌํ•ญ์€ ์ฝ”๋“œ๊ฐ€ ๋” ํ˜•์‹ํ™”๋˜๊ณ  ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ ํ•ด๋‹น div ์•ˆ์— ๋ฐฐ์น˜๋œ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ์šฐ๋ฆฌ์˜ ์„ ๋ฌผ์ธ HTML ๋‚ด์— div ์ฒด์ธ์„ ๊ฐ–๊ฒŒ ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์œ ์ง€:

<div id="app">
        <div>
            <div>
                <div>
                    <div>
                        <p>Component 4</p>
                    </div>
                </div>
            </div>
        </div>
</div>


์ด์ œ ์ด๋Ÿฌํ•œ ๊ตฌ์„ฑ ์š”์†Œ์— ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜์˜ ๋‘ ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜์— ๋„ฃ์€ Null์ด ์†์„ฑ์„ ๋„ฃ๋Š” ๋ฐ ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ๋งํ–ˆ๋“ฏ์ด ์˜ˆ๋ฅผ ๋“ค์–ด id๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

function MyComponent1() {
        return React.createElement(
          "div",
          {id: 'components'},
          React.createElement(MyComponent3)
        );
      }


HTML:

<div id="app">
    <div>
        <div>
            <div id="components">
                <div>
                    <p>Component 4</p>
                </div>
            </div>
        </div>
    </div>
</div>


๋ธŒ๋ผ์šฐ์ € ์š”์†Œ ๊ฒ€์‚ฌ์—์„œ ์ด๊ฒƒ์„ ์ฐธ์กฐํ•˜์‹ญ์‹œ์˜ค.

์ด์ œ id ๋Œ€์‹  ํด๋ž˜์Šค๋ฅผ ๋„ฃ๊ฒ ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ React์—์„œ๋Š” ํด๋ž˜์Šค๋ฅผ ๋„ฃ์ง€ ์•Š๊ณ  HTML์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ className์„ ๋„ฃ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์„ ์ฐธ์กฐํ•˜์„ธ์š”.

function MyComponent1() {
        return React.createElement(
          "div",
          { className: "components" },
          React.createElement(MyComponent3)
        );
      }


์—ฌ๊ธฐ์„œ๋Š” ์†Œ๊ฐœ๋กœ createElement๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๋ณดํ†ต ๊ทธ ์ฝ”๋”ฉ์—์„œ๋Š” JSX๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ณ  ๊ทธ ์•ˆ์—์„œ class๋ผ๋Š” ๋‹จ์–ด๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์˜ˆ์•ฝ์–ด์ด๊ธฐ ๋•Œ๋ฌธ์— ์ถฉ๋Œ์ด ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— React๋Š” ์ด๋ฅผ className์œผ๋กœ ๋ณ€๊ฒฝํ•ด์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํƒœ๊ทธ์— ํด๋ž˜์Šค๋ฅผ ํฌํ•จ์‹œํ‚ค๋ ค๋ฉด className ์„ ์‚ฌ์šฉํ•˜์‹ญ์‹œ์˜ค.

์™„๋ฃŒ๋œ ๋ชจ๋“  ์ž‘์—…์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

function MyComponent1() {
        return React.createElement(
          "div",
          { className: "components-1" },
          React.createElement(MyComponent2)
        );
      }
      function MyComponent2() {
        return React.createElement(
          "div",
          { className: "components-2" },
          React.createElement(MyComponent3)
        );
      }
      function MyComponent3() {
        return React.createElement(
          "div",
          { className: "components-3" },
          React.createElement(MyComponent4)
        );
      }
      function MyComponent4() {
        return React.createElement(
          "p",
          { className: "components-4" },
          "Component 4"
        );
      }

            function MyComponent() {
        return React.createElement(
          "div",
          { id: "components" },
          React.createElement(MyComponent1)
        );
      }


HTML:

<div id="app">
    <div id="components">
        <div class="components-1">
            <div class="components-2">
                <div class="components-3">
                    <p class="components-4">Component 4</p>
                </div>
            </div>
        </div>
    </div>
</div>


์—ฌ๊ธฐ์— ์˜จ ๊ฒƒ์„ ์ถ•ํ•˜ํ•ฉ๋‹ˆ๋‹ค. ๋งค์šฐ ๊ฐ•๋ ฌํ•˜๊ฒŒ ์ฝ์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์ด ์ตœ์ข… ์ฝ”๋“œ์˜€์Šต๋‹ˆ๋‹ค.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React JS - BEST GUIDE for Beginners!</title>
  </head>
  <body>
    <div id="app"></div>
    <script
      crossorigin
      src="https://unpkg.com/react@16/umd/react.development.js"
    ></script>
    <script
      crossorigin
      src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
    ></script>

    <script>
      function MyComponent1() {
        return React.createElement(
          "div",
          { className: "components-1" },
          React.createElement(MyComponent2)
        );
      }
      function MyComponent2() {
        return React.createElement(
          "div",
          { className: "components-2" },
          React.createElement(MyComponent3)
        );
      }
      function MyComponent3() {
        return React.createElement(
          "div",
          { className: "components-3" },
          React.createElement(MyComponent4)
        );
      }
      function MyComponent4() {
        return React.createElement(
          "p",
          { className: "components-4" },
          "Component 4"
        );
      }

      function MyComponent() {
        return React.createElement(
          "div",
          { id: "components" },
          React.createElement(MyComponent1)
        );
      }

      ReactDOM.render(
        React.createElement(MyComponent),
        document.getElementById("app")
      );
    </script>
  </body>
</html>


๊ทธ๋ฆฌ๊ณ  ์ง€๊ธˆ์€ ๊ทธ๊ฒŒ ๋‹ค์ž…๋‹ˆ๋‹ค. ๋‹ค์Œ ๊ธฐ์‚ฌ๋Š” ๊ฐ€๋Šฅํ•œ ํ•œ ๋นจ๋ฆฌ ๋‚˜์˜ฌ ๊ฒƒ์ž…๋‹ˆ๋‹ค! ๊ทธ๋ฆฌ๊ณ  ๊ทธ ์•ˆ์—์„œ ์šฐ๋ฆฌ๋Š” ์ด๋Ÿฌํ•œ ์ปดํฌ๋„ŒํŠธ ๊ธฐ๋ฐ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์™€ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ฃผ์š” ์–ด๋ ค์›€์ธ ๋‹จ๋ฐฉํ–ฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„(One-way binding)์„ ๋‹ค๋ฃฐ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ตฌ์„ฑ ์š”์†Œ ์ฒด์ธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ ๊ตฌ์„ฑ ์š”์†Œ ๊ฐ„์— ๊ฐ’์„ ์ „๋‹ฌํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ?

๋‹ค์Œ ๊ธฐ์‚ฌ์—์„œ ์ด ์งˆ๋ฌธ๊ณผ ๋‹ค๋ฅธ ์งˆ๋ฌธ์— ๋‹ตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋†€๋ผ์šด ๋‚ด์šฉ์„ ๋ณด๋ ค๋ฉด ์ €๋ฅผ ๊ณ„์† ํŒ”๋กœ์šฐํ•˜๊ณ  ์—ฌ๊ธฐ๋ฅผ ๋ฐฉ๋ฌธํ•˜์„ธ์š”!

ํŠธ์œ„ํ„ฐ์—์„œ ๋‚˜๋ฅผ ํŒ”๋กœ์šฐํ•˜์„ธ์š”:

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ