1. JSX
- ํจ์ ํธ์ถ๊ณผ ๊ฐ์ฒด ์์ฑ์ ์ํ ๋ฌธ๋ฒ์ ํธ์๋ฅผ ์ ๊ณตํ๋ JavaScript์ ํ์ฅ
- HTML๊ณผ ๋น์ทํ๊ฒ ์๊ฒผ์ผ๋ JavaScript์ด๊ณ , HTML๊ณผ ๋ค๋ฅธ ๋ถ๋ถ์ด ์๋ค.
- JSX๋ Babel์ ์ํด์ Transcompile ๋๋ค.
1-1. JSX์ ์ฅ์
1) ๊ฐ๋ฐ์ ํธ์์ฑ ํฅ์
2) ํ์ ์ ์ฉ์ด / ์์ฐ์ฑ ํฅ์
3) ๋ฌธ๋ฒ ์ค๋ฅ์ ์ฝ๋๋ ๊ฐ์
1-2. JSX์ ํน์ง (HTML๊ณผ ์ฐจ์ด์ )
1) HTML ํ๊ทธ ๋ด์ JavaScript ์ฐ์ฐ
const App = () => {
const a = 3;
const b = 6;
return <div>{a} + {b} = {a+b}</div>
}
2) class โก className
(
<div className="greeting">{name}๋ ์๋
ํ์ธ์.</div>
)
3) ์คํ์ผ์ object๋ก ์์ฑ
(
<div className="greeting" style={{ padding: 10, color: 'red' }}>{name}๋ ์๋
ํ์ธ์.</div>
)
// ๋ฐ๊นฅ ๊ดํธ : JavaScript ์ฝ๋์์ ์๋ฆฌ๋ ๊ดํธ
// ์์ชฝ ๊ดํธ : JavaScript Object์ ์์์์ ์๋ฆฌ๋ ๊ดํธ
- ์์ ๊ฐ์ Inline style์ property name์ camelCase๋ก ์ ๋๋ค. (ex. font-size โก fontSize)
4) ๋ซ๋ ํ๊ทธ ํ์
<div>
<input type="text" />
<br />
</div>
5) ์ต์๋จ element๋ ๋ฐ๋์ ํ๋
// ์๋ชป๋ ์์
const App = () => {
return (
<div>Hello</div>
<div>World</div>
)
}
// ์ฌ๋ฐ๋ฅธ ์์
const App = () => {
return (
<>
<div>Hello</div>
<div>World</div>
</>
)
}
- JSX์ ์์น์ ์ต์๋จ Element๋ ํ ๊ฐ๋ง ์์ฑ์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ์, ์ด๋ฅผ <div> ๋๋ <React.Fragment>๋ฅผ ์ด์ฉํด์ ๊ฐ์ธ์ค์ผ ํ๋ค.
- ์ค์ ๋ ๋๋ง ์์๋ Fragment ์์ ์๋ ๋ด์ฉ๋ง ์ถ๋ ฅ๋๋ค.
- <React.Fragment>๋ ๊ฐ๋จํ <>๋ก ํ๊ธฐ๊ฐ ๊ฐ๋ฅํ๋ค.
2. Component
- React์์ ํ์ด์ง๋ฅผ ๊ตฌ์ฑํ๋ ์ต์ ๋จ์
- ์ฌ์ฉ์ ์ ์ ํ๊ทธ
1) Component์ ์ด๋ฆ์ ๋๋ฌธ์๋ก ์์
const MyComponent = ({ children }) => {
return <div style={{padding: 20, color: "blue"}}>
{children}
</div>;
}
2) Class Component / Function Component๋ก ๋๋จ
- ์ด๊ธฐ React์ Component๋ ๋ชจ๋ Class Component์์ผ๋, v16 ๋ถํฐ ์๋ก์ด Function Component์ Hooks ๊ฐ๋ ์ด ๋ฐํ๋์ด ํ์ฌ๋ Function Component๊ฐ ์ฃผ๋ก ์ฌ์ฉ๋๊ณ ์๋ค.
/* Class */
class Hello extends Component {
render() {
const { name } = this.props
return <div>{name}๋ ์๋
ํ์ธ์.</div>
}
}
/* Function */
const Hello = (props) => {
const { name } = props
return <div>{name}๋ ์๋
ํ์ธ์.</div>
}
- Class Component ํน์ง
- Class ๊ฐ๋ ์ด ๋ง์ด ํ์ฉ๋๋ Java ๊ฐ๋ฐ์์๊ฒ ์น์ํ ํํ์ด๋ค.
- React์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ํ์ ํ๊ธฐ ์ฝ๋ค.
3) Controlled Component / Uncontrolled Component
2-1. Component์ ํน์ง
<MyComponent user={{name: "๋ฏผ์"}} color="blue">
<div>์๋
ํ์ธ์.</div>
</MyComponent>
์ปดํฌ๋ํธ์์ Attribute์ ํด๋นํ๋ ๋ถ๋ถ์ Props(Properties)๋ผ๊ณ ํ๋ค.
์ปดํฌ๋ํธ ์์ ์์ฑ๋ ํ์ Element๋ฅผ children์ด๋ผ๊ณ ํ๋ค. ๊ทธ๋ฆฌ๊ณ children๋ ๊ฒฐ๊ตญ์ props ์ค ํ๋์ด๋ค.
์์ ์์์์ ๋ณด์์ ๋, user={{name: "๋ฏผ์"}} color="blue" ๋ถ๋ถ์ด Props๊ฐ ๋๊ณ , <div>์๋ ํ์ธ์.</div> ๋ถ๋ถ์ด children์ด ๋๋ค.
const MyComponent = (props) => {
const { user, color, children } = props
return (
<div style={{ color }}>
<p>{user.name}๋์ ํ์ element๋</p>
{children}
</div>
)
}
์ ์ฝ๋๋ ์์ Element ๋ก๋ถํฐ ์ ๋ฌ๋ฐ์ props ๋ฅผ ํ์ฉํ๋ ์ฝ๋์ด๋ค.
์ด ์ปดํฌ๋ํธ์ ์์(children) ์์ ์ญ์ props ๋ก๋ถํฐ ๊ฐ์ ๋ฐ์์ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
โจ Component์ ๋ฐ์ดํฐ ๊ด๋ฆฌ
1. ์ปดํฌ๋ํธ๋ผ๋ฆฌ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ๋๋ Props
2. ์ปดํฌ๋ํธ ๋ด์์ ๋ฐ์ดํฐ๋ฅผ ๊ด๋ฆฌํ ๋๋ State
3. ๋ฐ์ดํฐ๋ ๋ถ๋ชจ ์ปดํฌ๋ํธ โก ์์ ์ปดํฌ๋ํธ๋ก๋ง ์ ๋ฌ
'elice > WIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[week_08] React ๊ธฐ์ด โ (4) React ํ๋ก์ ํธ ์์ฑ (0) | 2022.02.28 |
---|---|
[week_08] React ๊ธฐ์ด โ (3) Props์ State (0) | 2022.02.28 |
[week_08] React ๊ธฐ์ด โ (1) OT (0) | 2022.02.28 |
[week_07] 3๊ณ์ธต ์ค๊ณ (3-Layer architecture) (0) | 2022.02.26 |
[week_07] Express.js์ MongoDB๋ก ์น์๋น์ค ๋ง๋ค๊ธฐ (3) | JWT, OAuth, Nginx (0) | 2022.02.25 |