JSX๋ JavaScript
XML์ ์ฝ์๋ก ํจ์ ํธ์ถ๊ณผ ๊ฐ์ฒด ์์ฑ์ ์ํ ๋ฌธ๋ฒ์ ํธ์๋ฅผ ์ ๊ณตํ์ฌ UI ๊ฐ๋ฐ์ ํธ์์ฑ์ ๋๋ผ๊ฒ ํด์ฃผ๋ JavaScript ๋ฌธ๋ฒ์ ํ์ฅ๋ณธ์ด๋ค.
โ XML์ HTML์ ํ๊ณ๋ฅผ ๊ทน๋ณตํ๊ธฐ ์ํด ๋ง๋ค์ด์ง ๋งํฌ์ ์ธ์ด
HTML ํ๊ทธ ๋ด์ JavaScript ์ฐ์ฐ
class โ className
์คํ์ผ์ object์ด๋ฉฐ camelCase๋ก ์์ฑ
๋ซ๋ ํ๊ทธ ํ์ ex) <br />
์ต์๋จ element๋ ๋ฐ๋์ ํ๋๋ง ์์ฑ ๊ฐ๋ฅ, <div> ๋๋ <React.Fragment>๋ฅผ ์ฌ์ฉํ๊ณ ์ค์ ๋ ๋๋ง ์์๋ ๋ Fragment ์์ ์๋ ๋ด์ฉ๋ง ์ถ๋ ฅ. <React.Fragment>๋ ๊ฐ๋จํ <> ๋ก ํ๊ธฐ ๊ฐ๋ฅ.
JSX ์ฝ๋๋ ๋ธ๋ผ์ฐ์ ๊ฐ ์ดํดํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ Babel์ ์ํด์ Transcompile๋จ
const App = () => {
return (
<div className="App">
<header>
<img src={logo} className="App-logo" alt="logo" />
<h1>Welcome to React</h1>
</header>
<p style={{ color: 'blue', bakcgroundColor: 'black' }}>
Hello World
</p>
</div>
)
}
React์์ ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ ์ต์๋จ์๋ก ์ฌ์ฉ์๊ฐ ๋ง๋ค ์ ์๋ ํ๊ทธ์ด์ ์ฌ์ฌ์ฉ ํ ์ ์๋ UI ํจ์์ด๋ค.
Component์ ์ด๋ฆ์ ๋๋ฌธ์๋ก ์์
Class Component(ํด๋์คํ ์ปดํฌ๋ํธ) / Function Component(ํจ์ํ ์ปดํฌ๋ํธ) ๋ก ๋๋จ
Class Component ํน์ง
- Class ๊ฐ๋ ์ด ๋ง์ด ํ์ฉ๋๋ Java ๊ฐ๋ฐ์์๊ฒ ์น์ํ ํํ์ด๋ค.
- React์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ํ์ ํ๊ธฐ ์ฝ๋ค.
Controlled Component / Uncontrolled Component
์ปดํฌ๋ํธ์ Attribute์ ํด๋นํ๋ ๋ถ๋ถ์ Props(Properties)๋ผ๊ณ ํ๋ค. ์ปดํฌ๋ํธ ์์ ์์ฑ๋ ํ์ Element๋ children๋ผ๊ณ ํ๋๋ฐ children๋ ๊ฒฐ๊ตญ์ props ์ค ํ๋์ด๋ค.
์๋ฆฌ๋จผํธ๋ React ์ฑ์ ๊ฐ์ฅ ์์ ๋จ์๋ก ์ปดํฌ๋ํธ์ ๊ตฌ์ฑ์์์ด๋ค. ์๋ฆฌ๋จผํธ๋ ํ๋ฉด์ ํ์ํ ๊ฒ๋ค์ ๊ธฐ๋กํ๋ ๊ฒ์ผ๋ก ์ฝ๊ฒ ๋งํ์๋ฉด HTML ์์๋ผ๊ณ ํ ์ ์๋ค.
์ปดํฌ๋ํธ ์์์ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ฅผ ์ฐธ์กฐํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
function Introduce() {
return <h2>Hi, I am elice!</h2>;
}
ReactDOM.render(<Introduce />, document.getElementById('root'));
class Introduce extends React.Component {
render() {
return <h2>Hi, I am elice!</h2>;
}
}
ReactDOM.render(<Introduce />, document.getElementById('root'));
โโ React 16.8 ๋ฒ์ ๋ถํฐ Function Component์์ Hook์ ํตํด State๋ฅผ ์ฌ์ฉํ ์ ์๋ค.