728x90
* ๋ ธ๋ง๋์ฝ๋ ๊ฐ์ ์ ๋ฆฌ ๊ธ ์ ๋๋ค.
The BASICS OF REACT
- ๋ฆฌ์กํธ ๊ฐ์ ์์ ์ JS์ ReactJS๋ฅผ ๋น๊ตํด๋ณธ๋ค.
์์ ๊ฐ์ด ํด๋ฆญ ์ด๋ฒคํธ๊ฐ ์๋ ์นด์ดํธ ํ๋ก๊ทธ๋จ์ ๋ง๋ ๋ค๊ณ ์น์.
JavaScript
<!DOCTYPE html>
<html>
<body>
<h3>Total Clicks : 0</h3>
<button id="btn">Click me</button>
</body>
<script>
let counter = 0;
const button = document.getElementById('btn');
const h3 = document.querySelector('h3');
function handleClick(){
counter = counter + 1;
h3.innerText = `Total Clicks : ${counter}`;
}
button.addEventListener("click", handleClick);
</script>
</html>
โ HTML โ JS ์์
React 1 (createElement)
- ์ฌ์ด๋ฐฉ๋ฒ์ด ์์ง๋ง ๋ฆฌ์กํธ์ ๋์ ๋ฐฉ์์ ์ดํดํ๊ธฐ ์ํด์ ์ด๋ ค์ด ๋ฐฉ๋ฒ์ผ๋ก ์ฝ๋ ์์ฑ
- ์ง๊ธ ํ๋ ๋ฐฉ๋ฒ์ ์๊ธฐํ ํ์๋ ์์
<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<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>
<script>
const root = document.getElementById("root");
//React๋ก Element ์์ฑ("span", {span์ property}, "๋ด์ฉ")
const h3 = React.createElement(
"h3",
{ id : "title",
onMouseEnter : () => console.log('mouse enter')},
"Hello I'm a Title"
);
const btn = React.createElement(
"button",
{ onClick : () => console.log('im clicked')},
"Click me"
);
const div = React.createElement("div", null, [span, btn]);
ReactDOM.render(div, root);
</script>
</html>
* React : UI๋ฅผ ๋ง๋ค ์ ์๊ฒํ๋ ์์ง
* React-dom : ๋ชจ๋ react element๋ค์ HTML body์ ๋ ์ ์๋๋ก ํด์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋๋ ํจํค์ง
โ JS โ HTML ์์
React 2 (JSX)
- JSX๋ Javascript๋ฅผ ํ์ฅํ ๋ฌธ๋ฒ
<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<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>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
const root = document.getElementById("root");
const Title = () => (
<h3 id="title" onMouseEnter={() => console.log("mouse enter")}>
Hello I'm a Title
</h3>
);
const Button = () => (
<button onClick = {() => console.log('im clicked')}>
Click me
</button>
);
const Container = () => (
<div>
<Title />
<Button />
</div>
);
ReactDOM.render(<Container />, root);
</script>
</html>
โ ์ฌ๊ธฐ์ ์ค์ํ ๊ฑด ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค ๋ ์๊ธ์๊ฐ ๊ผญ ๋๋ฌธ์์ฌ์ผ ํ๋ค๋ ๊ฒ!
๋ค์ ๊ฐ์์์ ๊ณ์
๋ฐ์ํ
'IT > React' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ์ ํฌ๊ธฐ๋ก ์๋ผ ๋จน๋ ๋ฆฌ์กํธ] - React.js ๊ฐ๋ก (1) | 2024.11.15 |
---|---|
[ํ์ ํฌ๊ธฐ๋ก ์๋ผ ๋จน๋ ๋ฆฌ์กํธ] - Node.js ๊ธฐ์ด (5) | 2024.11.01 |
[React] ReactJS๋ก ์ํ ์น ์๋น์ค ๋ง๋ค๊ธฐ (STATE #1) (0) | 2024.02.22 |