Frontend/React

리액트 연습하기

전예방 2022. 11. 16. 23:37

리액트를 웹사이트에서 연습해보자

 

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Static Template</title>
  </head>
  <body>
    <h1>This is a static template, there is no bundler or bundling involved!</h1>
  </body>
</html>

<!-- Javascript -->
<!DOCTYPE html>
<html lang="en">
  <body>
    <div id="root"></div>
    <script>
      const rootElement = document.getElementById("root");
      const element = document.createElement("h1");
      element.textContent = "Hello Javascript!";

      rootElement.appendChild(element);
    </script>
  </body>
</html>

<!-- React -->
<html lang="en">
  <body>
    <script
      src="https://unpkg.com/react@18/umd/react.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
      crossorigin
    ></script>
    <div id="root"></div>
    <script>
      const rootElement = document.getElementById("root");
      const element = React.createElement("h1", {
        children: "Hello, React!"
      });

      ReactDOM.render(element, rootElement);
    </script>
  </body>
</html>

<!-- JSX -->
<html lang="en">
  <body>
    <script
      src="https://unpkg.com/react@18/umd/react.development.js"
      crossorigin
    ></script>
    <script
      src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"
      crossorigin
    ></script>
    <script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
    <div id="root"></div>
    <script type="text/babel">
      const rootElement = document.getElementById("root");

      const text = "Hello JSX!";
      const className = "title";

      const element = <h1 className={className}>{text}</h1>;

      ReactDOM.render(element, rootElement);
    </script>
  </body>
</html>

 

JSX를 사용하기 위해서

 

// 바벨을 추가해주고
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>

// 타입을 지정해줘야한다!!
<script type="text/babel">

 

'Frontend/React'의 다른글

  • 현재글 리액트 연습하기

관련글