[react] reRendering(๋ฆฌ๋ Œ๋”๋ง)

12617 ๋‹จ์–ด ReactReact

๐ŸŸข ๋ฆฌ์•กํŠธ์˜ ํŠน์ง•

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๋‹ฌ๋ฆฌ ๋ฆฌ์•กํŠธ๋Š” UI์—์„œ ๋ฐ”๋€ ๋ถ€๋ถ„๋งŒ ์—…๋ฐ์ดํŠธํ•จ

<body>
    <span> Total clicks: 0</span>
    <button id="btn">click me</button>
  </body>

js์—์„œ๋Š” body์™€ span์ด ํ†ต์งธ๋กœ ์—…๋ฐ์ดํŠธ ๋จ

<div>
	<h3>Total clicks: {counter}</h3>
    <button onClick={countUp}>Click me</button>
</div>

react์—์„œ๋Š” {counter} ์†๋งŒ ์—…๋ฐ์ดํŠธ ๋จ


๐ŸŸข ์˜ˆ์ œ

<script type="text/babel">
      const root = document.getElementById("root");
      let counter = 0;

      function countUp() {
        counter = counter + 1;
        render();
      }
      function render() {
        ReactDOM.render(<Container />, root);
      }
      function Container() {
        return (
          <div>
            <h3>Total clicks: {counter}</h3>
            <button onClick={countUp}>Click me</button>
          </div>
        );
      }
      render();
    </script>

์ˆœ์„œ

  1. let counter = 0
  1. render()
    ReactDom.render(, root) ์‹คํ–‰
  1. Container ๋ Œ๋”๋ง(์ฒซ๋ฒˆ์งธ ๋ Œ๋”๋ง)
    Container() ์† {counter} = 0
  1. onClick={countUp}
    countUp() ์‹คํ–‰
  1. counter = 1
  1. render() ์‹คํ–‰
    ReactDOM.renderender(, root)
  1. Container ๋ Œ๋”๋ง(๋‘๋ฒˆ์žฌ ๋ Œ๋”๋ง)
    counter = 1 ์—…๋ฐ์ดํŠธ

๐ŸŸข ์ „์ฒด ์ฝ”๋“œ

<!DOCTYPE html>
<html>
  <body>
    <div id="root"></div>
    <script src="https://unpkg.com/react@17.0.2/umd/react.development.js"></script>
    <script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
    <script src="https://unpkg.com/@babel/standalone@7.17.6/babel.min.js"></script>
    <script type="text/babel">
      const root = document.getElementById("root");
      let counter = 0;

      function countUp() {
        counter = counter + 1;
        render();
      }
      function render() {
        ReactDOM.render(<Container />, root);
      }
      function Container() {
        return (
          <div>
            <h3>Total clicks: {counter}</h3>
            <button onClick={countUp}>Click me</button>
          </div>
        );
      }
      render();
    </script>
  </body>
</html>

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