์ง์์ ์ผ๋ก ์ฐธ๊ณ ํ๊ณ ์ถ์ ๋ฆฌ์กํธ์ ์ฒ ํ์ ์ธ ๋ด์ฉ์ ์ ๋ฆฌํ๋ ๊ฒ์ด ๋ชฉํ
๊ฐ์ฅ ๊ฐ๋จํ React ์์
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
const element = <h1>Hello, world!</h1>;
์ ์ฒ์ ์ผ๋ก ๋ ๋๋ง ๋ก์ง์ ๋ค๋ฅธ ์ฌ๋ฌ ๋ก์ง๋ค๊ณผ ์ฐ๊ฒฐ์ด ๋์ด์๋ค. (์ด๋ฒคํธ ๊ด๋ฆฌ, ์๊ฐ์ ๋ฐ๋ฅธ state ๋ณํ, ๋ฐ์ดํฐ ์ค๋น ์ํฉ ๋ฑ) React๋ ์ด ์ฌ์ค์ ํฌ์ฉํ๋ ์๋ฏธ์์ JSX๋ฅผ ์ฌ์ฉํด์ ๋ก์ง๊ณผ ๋งํฌ์ ์ ํ ๊ณณ์ ๋ด๊ณ ์ ํ ๊ฒ์ด๋ค. (๋ก์ง๊ณผ ๋งํฌ์ ์ ํ ๊ณณ์ ๋ฌถ์ ๊ณณ์ด ๋ฐ๋ก "components"์ด๋ค.)
๊ทธ๋์ JSX๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ ์ฌ์ค ํ์๊ฐ ์๋๋ค. ๋๋ถ๋ถ ๋ ํธํ๊ฒ ์ฌ๊ธธ ๋ฐ๋ฆ์ด๋ค.
๋ณํ ํ์๋ JSX๋ ๋จ์ง ํ๋์ JavaScript object์ผ ๋ฟ์ด๋ค. ๋ฐ๋ผ์ if๋ฌธ, for๋ฌธ, ๋ณ์ ํ ๋น, arguments, return ๋ฑ๋ฑ ์์ ์ฌ์ฉํ ์ ์๋ค.
function getGreeting(user) {
if (user) {
return <h1>Hello, {formatName(user)}!</h1>;
}
return <h1>Hello, Stranger.</h1>;
}
string literal๋ก ์์ฑ์ ๋ช ์ํ๋ค.
const element = <div tabIndex="0"></div>;
์ค๊ดํธ๋ก ๊ฐ์ธ JavaScript ํํ์์ ์์ฑ์ผ๋ก ์ฌ์ฉํ ์๋ ์๋ค.
const element = <img src={user.avatarUrl}></img>;
* ํ ์์ฑ์ ๋ช
์ํ ๋ ๋ฐ์ดํ(string literal)์ด๋ ์ค๊ดํธ ๋ ์ค ํ๋๋ง ์ฌ์ฉํด์ผ ํ๋ค.
* ์์ฑ ๊ฐ์ camelCase๋ฅผ ์ฌ์ฉํ๋ค. JSX๋ HTML๋ณด๋ค๋ JavaScript์ ๊ฐ๊น๊ธฐ ๋๋ฌธ์ด๋ค.
const title = response.potentiallyMaliciousInput;
// This is safe:
const element = <h1>{title}</h1>;
React๋ JSX์์ ์ด๋ค ๊ฐ์ด๋ escapeํ๋ค. ๋ชจ๋ ๊ฒ๋ค์ ๋ ๋๋ง ๋๊ธฐ์ ๋ฌธ์์ด๋ก ๋ณํ๋๋ค. ๋ฐ๋ผ์ ์ธ์ ์ ๊ณต๊ฒฉ์ ํ ์ ์๋ค. XSS(cross-site-scripting) ๊ณต๊ฒฉ๋ ๋ง์์ค๋ค.
Babel์ JSX๋ฅผ React.createElement()
๋ก ์ปดํ์ผ ํ๋ค.
const element = (
<h1 className="greeting">
Hello, world!
</h1>
);
const element = React.createElement(
'h1',
{className: 'greeting'},
'Hello, world!'
);
์ ๋ ์์ ๋ ์์ ํ ๋์ผํ๋ค.
๊ทธ๋ฆฌ๊ณ React.createElement()
๋ ๋ฒ๊ทธ๊ฐ ์๋์ง ๋ช ๊ฐ์ง ์ฒดํฌ๋ฅผ ํ ํ, ์๋์ ๊ฐ์ object๋ฅผ ๋ง๋ค์ด๋ธ๋ค.
// ๊ฐ๋ตํ ๋ฒ์ ์
const element = {
type: 'h1',
props: {
className: 'greeting',
children: 'Hello, world!'
}
};
์ด object๋ฅผ "React elements"๋ผ๊ณ ๋ถ๋ฅธ๋ค. React๋ ์ด object์ ๋ด์ฉ์ ์ฝ์ด DOM์ ๊ทธ๋ฆฌ๊ณ ๋ณํ๋ฅผ ์ถ์ ํ๋๋ฐ ์ฌ์ฉํ๋ค.
const element = <h1>Hello, world</h1>;
React elements๋ immutableํ๋ค. ์ผ๋จ element๋ฅผ ์์ฑํ์ผ๋ฉด, children์ด๋ attribute๋ฅผ ์์ ํ๋๊ฑด ๋ถ๊ฐ๋ฅํ๋ค. element๋ ์ํ์์ ํ๋์ ํ๋ ์๊ณผ ๊ฐ๋ค.
๋ฐ๋ผ์ ์๋์ ๊ฐ์ด ๋งค๋ฒ ReactDOM.render๋ฅผ ํธ์ถํ๋ ๊ฒ์ด ํ์ฌ๊น์ง ์๋ ์ ์ผํ UI ์ ๋ฐ์ดํธ ๋ฐฉ๋ฒ์ด๋ค.
function tick() {
const element = (
<div>
<h1>Hello, world!</h1>
<h2>It is {new Date().toLocaleTimeString()}.</h2>
</div>
);
ReactDOM.render(element, document.getElementById('root'));
}
setInterval(tick, 1000);
| ๋ค๋ง, ์ค์ React App์ ReactDOM.render()๋ฅผ ํ๋ฒ๋ง ํธ์ถํ๋ค. state๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ธ๋ฐ ๋ค์ ๋์จ๋ค.
์ปดํฌ๋ํธ๋ UI๋ฅผ ๋ ๋ฆฝ์ ์ด๊ณ , ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ์กฐ๊ฐ์ผ๋ก ๋๋๊ฒ ํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ ์กฐ๊ฐ์ ๋ ๋ฆฝ์ ์ผ๋ก ์ฌ๊ธฐ๊ฒ ํ๋ค. ๊ฐ๋ ์ ์ผ๋ก, components๋ JavaScript ํจ์์ ๊ฐ๋ค.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;
React๊ฐ ์ฌ์ฉ์ ์ ์ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋๋ฉด, JSX ์์ฑ๊ณผ children์ "props"๋ผ๋ ์ด๋ฆ์ object์ ๋ด์ ์ ๋ฌํ๋ค.
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
const element = <Welcome name="Sara" />;
ReactDOM.render(
element,
document.getElementById('root')
);
!!์ปดํฌ๋ํธ์ ์ด๋ฆ์ ๋ฌด์กฐ๊ฑด ๋๋ฌธ์๋ก ์์ํด์ผํ๋ค.!!
ํ ์ปดํฌ๋ํธ๋ ๋ค๋ฅธ ์ปดํฌ๋ํธ์์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฏ๋ก, ์๋ฅผ๋ค์ด button, form, dialog ๋ฑ์ ์ด๋ ๋จ๊ณ์์๋ ์ฌ์ฉํ ์ ์๋ค.
Welcome ์ปดํฌ๋ํธ๋ฅผ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ App
component
function Welcome(props) {
return <h1>Hello, {props.name}</h1>;
}
function App() {
return (
<div>
<Welcome name="Sara" />
<Welcome name="Cahal" />
<Welcome name="Edite" />
</div>
);
}
ReactDOM.render(
<App />,
document.getElementById('root')
);
๋ง์ฝ, ๊ธฐ์กด์ ์ฑ์ React๋ก ๋์ฒดํ๋ค๋ฉด bottom-up์ผ๋ก ๊ฐ์ฅ ์์ ์ปดํฌ๋ํธ ๋ถํฐ ์กฐ์ฑํด๋๊ฐ๋ฉด์ ์์ ์ ์งํํ ์ ์์ ๊ฒ์ด๋ค.
์๋ ํจ์๋ "pure"ํ๋ค. ์ ๋ ฅ๊ฐ์ ๋ณํ์ํค์ง ์๊ณ ๊ฐ์ ์ ๋ ฅ๊ฐ์ ํญ์ ๊ฐ์ ์ถ๋ ฅ์ ๋ด๋ณด๋ด๊ธฐ ๋๋ฌธ์ด๋ค.
function sum(a, b) {
return a + b;
}
function withdraw(account, amount) {
account.total -= amount;
}
React์๋ ์ ์ฐํ์ง๋ง ํ๋์ ์๊ฒฉํ ๊ท์น์ด ์๋ค.
All React components must act like pure functions with respect to their props.
๋ค๋ง UI๋ ๋ค์ด๋๋ฏนํ๊ฒ ๋ณํํด์ผํ๋ค. ๊ทธ๋์ state๋ฅผ ์ด์ฉํ ๊ฒ์ด๋ค.