react에서 사용하는 문법
=> 브라우저에서 실행하기 전에 바벨을 사용하여 일반 자바스크립트 형태의 코드로 변환함.
=> 하나의 파일에 js와 html을 동시 작성 가능하여 편리함.
// 1) 실제 작성할 JSX 예시
function App() {
return (
<h1>Hello, Eunseo!</h1>
);
}
// 2) 바벨이 자바스크립트로 해석하여 준다.
function App() {
return React.createElement("h1", null, "Hello, Eunseo!");
}
function App() {
return (
<p>Hello world // 에러발생
<p>Hello world</p> // 정상작동
<input type='text' > 에러발생
<input type='text' /> // 정상작동
);
}
//1) 에러 케이스
function App() {
return (
<div>Hello</div>
<div>Eunseo!</div>
);
}
// 2) div
function App() {
return (
<div>
<div>Hello</div>
<div>Eunseo!</div>
</div>
);
}
// 3) Fragment
function App() {
return (
<Fragment>
<div>Hello</div>
<div>Eunseo!</div>
</Fragment>
);
}
// 4) <> </>
function App() {
return (
<>
<div>Hello</div>
<div>Eunseo!</div>
</>
);
}
function App() {
const name = 'Eunseo';
return (
<div>
<div>Hello</div>
<div>{name}!</div>
</div>
);
}
//1) 외부에서 사용
function App() {
let desc = '';
const loginYn = 'Y';
if(loginYn === 'Y') {
desc = <div>Eusneo 입니다.</div>;
} else {
desc = <div>비회원 입니다.</div>;
}
return (
<>
{desc}
</>
);
}
//2) 내부에사 사용
function App() {
const loginYn = 'Y';
return (
<>
<div>
{loginYn === 'Y' ? (
<div>Eunseo 입니다.</div>
) : (
<div>비회원 입니다.</div>
)}
</div>
</>
);
}
// 3) AND 연산자 사용
function App() {
const loginYn = 'Y';
return (
<>
<div>
{loginYn === 'Y' && <div>Eunseo 입니다.</div>}
</div>
</>
);
}
// 4) 즉시 실행 함수
function App() {
const loginYn = 'Y';
return (
<>
{
(() => {
if(loginYn === "Y"){
return (<div>Eunseo 입니다.</div>);
}else{
return (<div>비회원 입니다.</div>);
}
})()
}
</>
);
}
function App() {
const style = {
backgroundColor: 'green',
fontSize: '12px'
}
return (
<div className="testClass">Hello, Eunseo!</div>
);
}
import 이용
import './App.css'