순수 HTML 문서로 React.js 프로그래밍
추천 자료: ASP.NET Core 인증 및 권한 부여
순수 HTML 문서로 React.js 프로그래밍 시작
코드: HelloReact00.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<h1>Hello, React.js!</h1>
</body>
</html>
React.js 관련 JavaScript 라이브러리 포함
코드: HelloReact01.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 01</title>
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
ReactDOM.render(React.createElement("h1", null, "안녕하세요."), document.getElementById("root"));
</script>
</body>
</html>
태그의 속성에 속성 값 주기
코드: HelloReact02.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 02</title>
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
ReactDOM.render(
React.createElement("h1", { "style": { "color": "blue"} }, "안녕하세요."),
document.getElementById("root")
);
</script>
</body>
</html>
중첩된 HTML 요소 작성
코드: HelloReact03.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 03</title>
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
ReactDOM.render(
React.createElement(
"div", { "style": { "border": "1px solid red " } },
React.createElement("h1", { "style": { "color": "blue"} }, "안녕하세요."),
React.createElement("h2", { "style": { "color": "red"} }, "안녕하세요."),
React.createElement("h3", { "style": { "color": "green"} }, "안녕하세요."),
),
document.getElementById("root")
);
</script>
</body>
</html>
Babel을 사용하여 JSX를 사용한 태그 생성
코드: HelloReact04.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 04</title>
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
// JSX를 사용하여 HTML을 JavaScript로 변환
ReactDOM.render(
<div><h1>안녕하세요.</h1></div>,
document.getElementById("root")
);
</script>
</body>
</html>
JSX의 여러 가지 특징 살펴보기
JSX의 HTML 태그는 반드시 시작과 끝이 명확해야 합니다.
코드: HelloReact05.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 05 - JSX</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
ReactDOM.render((
<div className="container mb-2">
<div className="row">
<div className="col-2">
C#
</div>
<div className="col-10">
내용
</div>
</div>
</div>
),
document.getElementById("root")
);
</script>
</body>
</html>
JSX 표현식 사용하기
코드: HelloReact06.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 06 - JSX Expression</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
let title = "JSX 표현식(Expression)";
let seeSharp = {
title: "C#",
description: "내용 들어오는 곳..."
};
ReactDOM.render((
<div className="container mb-2">
<div className="row">
<div className="col-12">
{title}
</div>
</div>
<div className="row">
<div className="col-2">
{seeSharp.title}
</div>
<div className="col-10">
{seeSharp.description}
</div>
</div>
</div>
),
document.getElementById("root")
);
</script>
</body>
</html>
함수 컴포넌트 만들기
코드: HelloReact07.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 07 - Function Component</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
동적 개체인 props를 사용하여 컴포넌트에 전달된 속성 이름과 속성 값을 받아 사용하기
코드: HelloReact08.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 08 - Props</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
구조 분해 할당을 사용하여 props를 좀 더 간결하게 사용하기
코드: HelloReact09.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 09 - 구조 파괴(분해) 할당(Destructuring Assignment)</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- <script src="./babel.min.js"></script> -->
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
JSX에서 조건(3항) 연산자 사용하기
코드: HelloReact10.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 10 - JSX에서 조건(3항) 연산자 사용하기</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- <script src="./babel.min.js"></script> -->
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
클래스로 컴포넌트 만들기
코드: HelloReact11.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 11 - 클래스 컴포넌트</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- <script src="./babel.min.js"></script> -->
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
상태 관리와 클릭 이벤트
코드: HelloReact12.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 12 - 상태 관리와 클릭 이벤트</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- <script src="./babel.min.js"></script> -->
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
컬렉션 형태의 데이터 출력
코드: HelloReact13.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello React 13 - 컬렉션 형태의 데이터 출력</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script crossorigin="anonymous" src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin="anonymous" src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<!-- <script src="./babel.min.js"></script> -->
</head>
<body>
<div id="root"></div>
<script type="text/babel"></script>
</body>
</html>
추천 자료: .NET Blazor에 대해 알아보시겠어요? .NET Blazor 알아보기를 확인해보세요!