Component
νμ΅ ν€μλ
Component
ν΄λμ€ μ»΄ν¬λνΈμ ν¨μν μ»΄ν¬λνΈ
μ»΄ν¬λνΈ μμ± κ·μΉ
JSX
Propsμ νμ©λ²
Component
리μ‘νΈλ μΉμ ꡬμ±μμλ₯Ό κ°μ₯ μμ λ¨μλ‘ μΈ μ»΄ν¬λνΈλ‘ ꡬμ±νλ€. μ΄λ¬ν μ»΄ν¬λνΈλ₯Ό μ‘°ν©ν΄μ νλμ νμ΄μ§λ‘ μμ±νκ³ , λ°λ³΅μ μΈ μΉμ ꡬμ±μμλ₯Ό μ¬μ¬μ© ν μ μλ€λ νΉμ§μ κ°μ§κ³ μλ€.
ν΄λμ€ μ»΄ν¬λνΈ μ ν¨μν μ»΄ν¬λνΈ
리μ‘νΈλ ν΄λνκ³Ό ν¨μν μ»΄ν¬λνΈ λκ°μ§ λ°©μμΌλ‘ μμ±ν μ μλ€. λκ°μ§ λ°©μ λͺ¨λ μ§μνκ³ μμ§λ§, 곡μμ μΌλ‘λ ν΄λμ€ μ»΄ν¬λνΈ λ°©μμ μ¬μ©νμ§ μλκ±Έ κΆμ₯νλ€. 리μ‘νΈ 16.8λ²μ μ μΆκ°λ 리μ‘νΈ ν
μΌλ‘ μΈν΄ ν¨μν μ»΄ν¬λνΈμ μ¬μ©μ±μ΄ κ°μ λμ΄ λμ΄μ ν΄λμ€ μ»΄ν¬λνΈλ₯Ό μ¬μ©νλ 건 λΉν¨μ¨μ μ΄λ€.
μ»΄ν¬λνΈ μμ± κ·μΉ
μ»΄ν¬λνΈ μμ±μ μ΄λ¦μ νμ λλ¬Έμλ‘ μμνλ€.
export default function App() {}
νμ΄ννμ, ν¨μμ μΈμ λλ€ μ¬μ© κ°λ₯νλ€.
export default function App() {}
const App = () => {};
export default App; //ν¨μ ννμμΌλ‘ μ¬μ©μ ν΄λΉ λ°©λ²μΌλ‘ λ΄λ³΄λ΄μΌ νλ€.
JSX
Javascript + XML
λ°λμ μ΅μμ λ£¨νΈ νκ·Έκ° μ‘΄μ¬ν΄μΌ νλ€
export default function App() {
return (
<>
<div>1</div>
<div>2</div>
</>
);
}
<></> === <React.Fragment></React.Fragment> 리μ‘νΈμμ μ 곡νλ Fragment
μ¬λ¬μ€μ μ½λλ₯Ό return ν κ²½μ° ()
μκ΄νΈ μ¬μ©
()
μκ΄νΈ μ¬μ©export default function App() {
return (
// ππ»
<>
<div>리μ‘νΈ</div>
<div>리μ‘νΈ2</div>
</>
);
}
λ°λμ νκ·Έλ λ«μμ€λ€
import Button from './components/Button';
export default function App() {
return (
<>
<Button />
<Button></Button>
</>
);
}
ννμμ {}
μ¬μ©νλ€
{}
μ¬μ©νλ€κ°μ²΄,λ°°μ΄,Boolean μ€κ΄νΈλ₯Ό μ¬μ©νλ λ°©μ λΆκ°λ₯
export default function Button() {
const text = 'λ²νΌμ
λλ€!';
return <div>{text}</div>;
}
Propsμ νμ©λ²
1. λ§€κ°λ³μ ꡬ쑰λΆν΄ν λΉ
βοΈ κ°μ₯ λ§μ΄ μ¬μ©νλ λ°©μ
export default function PropsButton({
type,
name,
disabled,
children,
}: TButtonProps) {
return (
<button type={type} name={name} disabled={disabled}>
{children}
</button>
);
}
2. μ»΄ν¬λνΈ λ΄μμ propsμ ꡬ쑰λΆν΄ν λΉ
export default function PropsButton(props: TButtonProps) {
const { type, disabled, children } = props;
return (
<button type={type} disabled={disabled}>
{children}
</button>
);
}
3.λλ¨Έμ§ λ§€κ°λ³μλ₯Ό νμ©ν ꡬ쑰λΆν΄ν λΉ
type TButtonProps = React.ComponentProps<'button'> & {
children: ReactNode,
};
export default function PropsButton(props: TButtonProps) {
const { children, ...restButtonProps } = props;
return <button {...restButtonProps}>{children}</button>;
}
Last updated