📅 2025-10-02
➡️ React JSX, props에 대해 새롭게 알게 된 것 또는 헷갈리는 부분 정리
{ }에는 표현식만 작성{ }를 이용해서 사용import React from 'react';
function App() {
const number = 1;
return (
<div className="App">
<p>안녕하세요</p>
{/* 삼항 연산자 사용 */}
<p>{number > 10 ? number+'은 10보다 크다': number+'은 10보다 작다'}</p>
</div>
);
}
export default App;❗컴포넌트에서 반환할 수 있는 엘리먼트는 1개
ClassNameclassName으로 사용function Mother() {
const name = "홍길동";
return <Child motherName={name} />; // name을 motherName 이라는 이름으로 전달
}
function Child(props) {
console.log(props); // { motherName: "홍길동" }
return <div>{props.motherName}</div>;
}children으로 정해져 있는 예약 속성function User(props) {
return <div>{props.children}</div>;
}
function App() {
return <User>안녕하세요</User>;
}
children으로 바꿀 수 있음//Layout.jsx
function Layout({ children }) {
return (
<div>
<header>공통 헤더</header>
<main>{children}</main>
</div>
);
}
export default Layout;
// App.jsx
import Layout from "./Layout";
function App() {
return (
<Layout>
<div>여기는 App 페이지의 컨텐츠</div>
</Layout>
);
}
// About.jsx
import Layout from "./Layout";
function About() {
return (
<Layout>
<div>여기는 About 페이지의 컨텐츠</div>
</Layout>
);
}function Todo(props){
return <div>{props.title}</div>
}function Todo ({title}){
return <div>{title}</div>
}function Welcome({ name = "Guest" }) {
return <h1>Welcome, {name}!</h1>;
}