1. React CDN 링크 추가 (head에 넣기)
<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
2. babel CDN 링크 추가
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
3. JSX 문법으로 HTML태그 생성
<body>
<div id="root"></div>
<script type="text/babel">
let a = 0;
const h1 = React.createElement("h1", { id: "h1", style: { color: "blue" } }, JSX 문법 사용);
const button = React.createElement("button", { onClick: () => {a = a+1; console.log(a); } }, Click!);
const container = React.createElement("div", null, [h1, button]);
ReactDOM.render(container, document.getElementById("root"))
</script>
</body>
화살표 함수
로 정의해도 됨<body>
<div id="root"></div>
<script type="text/babel">
const App = () => {
let a = 0;
return (
<div>
<h1 id='h1' style={{color:'blue'}}>
JSX 문법 사용
</h1>
<button onClick={() => {a = a+1; console.log(a);}}>
Click!
</button>
</div>
)
}
ReactDOM.render(container, document.getElementById("root"))
</script>
</body>
import React, { Component } from "react";
class App extends Component {
render() {
const name = "react";
return <h1 className="h1">{name}</h1>;
}
}
export default App;