React.js는 자바스크립트 라이브러리
싱글 페이지 어플리케이션 및 모바일 어플리케이션 개발 가능
초기 랜더링 → 가상 DOM 변경 → 재조정 → 실제 DOM 업데이트
DOM(Document Object Model)
웹 페이지의 구조화된 내용을 표현하고 상호작용할 수 있는 방법을 제공하는 인터페이스
npx create-react-app my-app
cd my-app
npm start
npx create-react-app todolist --template typescript
자바스크립트의 확장 문법으로, React 프로젝트를 개발할 때 사용
function App() {
return (
<div>
Hello <b>react</b>
</div>
);
}
function App() {
return React.createElement("div", null, "Hello ",
React.createElement("b", null, "react"));
컴포넌트에 여러 요소가 있다면 반드시 부모 요소 하나로 감싸야 함
function App() {
return (
<h1>hello</h1>
<h2>react</h2>
);
}
→ error 발생
function App() {
return (
<div> // <Fragment> or <>
<h1>hello</h1>
<h2>react</h2>
</div> // </Fragment> or </>
);
}
일반 HTML에서 CSS 클래스를 사용할 때는 div class="css"</div>
→ JSX에서는 className으로 설정
App.js
import "./App.css';
function App() {
const name = "react";
return <div className="react">{name}</div>
}
App.css
.react {
background-color: purple;
color: white;
font-size: 48px;
}
JSX 안에서 JavaScript를 사용하려면 코드를 { }로 감싸줌
function App() {
const name = 'react';
return (
<div>
<h1>hello {name}</h1>
);
}
JSX 내부의 자바스크립트 표현에서 조건에 따라 다른 내용을 렌더링하기 위해 사용
function App() {
const name = "react";
return (
<div>
{name === "react" ? (<h1>react ⭕️</h1>) : (<h1>react ❌</h1>)}
</div>
);
}
camelCase로 작성
function App() {
const name = "react";
const style = {
backgroundColor: "black",
color: 'white',
fontSize: '48px'
}
return (
<div style={style}>
...
}
function App() {
const name = "react";
return (
<div>
<input></input>
<input /> // self-closing
</div>
);
}
{/* 주석 */}