2024.01.31(수)
사용자 인터페이스(UI, User Interface)를 만들기 위한 JavaScript 라이브러리 🔗
SPA(Single Page Application) 기반
동작 원리
실제 화면과 똑같은 가상 화면을 메모리에 만들고 모든 연산을 가상 화면에서 진행한 후 완성된 화면을 실제 화면에 복사하는 기법
환경설정
npx create-react-app myblog --template typescript
cd myblog
npm start
자바스크립트 확장 문법 🔗
index.html ← index.tsx ← App.tsx
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>…</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
src/index.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
const root = ReactDOM.createRoot(
document.getElementById('root') as HTMLElement
);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
reportWebVitals();
src/App.tsx
import React from 'react';
import logo from './logo.svg';
import './App.css';
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Hello React!</h1>
</header>
</div>
);
}
export default App;
src/App.css
.App {
text-align: center;
}
.App-logo {
height: 40vmin;
pointer-events: none;
}
@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.App-link {
color: #61dafb;
}
@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
function App() {
return (
<div>
<div>Hello React!</div>
<p>만나서 반갑습니다.</p>
</div>
);
}
<></>
)로 감싸면 wrapper node 없이 요소들을 그룹화 가능 (<Fragment></Fragment>
의 shorthand 🔗)function App() {
return (
<>
<div>Hello React!</div>
<p>만나서 반갑습니다.</p>
</>
);
}
function App() {
return (
<Fragment>
<div>Hello React!</div>
<p>만나서 반갑습니다.</p>
</Fragment>
);
}
+) 가독성을 좋게 하기 위해 JSX를 여러 줄로 나누는데 혹시 모를 자동 세미콜론 삽입을 피하기 위해 괄호로 묶는 것이 좋다.
{}
)로 묶어서 포함 가능 (Data Binding)function App() {
const title = "Hello React!";
return (
<div>
<div>{title}</div>
<p>만나서 반갑습니다.</p>
</div>
);
}
⚠️ JSX는 HTML보다는 자바스크립트에 가깝기 때문에, React DOM은 HTML 속성 이름 대신 camelCase 속성 이름 컨벤션을 사용
예를 들어, JSX에서class
는className
,tabindex
는tabIndex
/* 클래스는 App.css에 정의되어 있음 */
function App() {
return (
<div className="App">
<header className="App-header">
<h1>Hello React!</h1>
</header>
</div>
);
}
const element = <div tabIndex="0"></div>;
const element = <img src={user.avatarUrl}></img>;
function App() {
const style = {
backgroundColor: "yellow",
color: "black",
fontSize: "50px"
};
return (
<div className="App">
<header className="App-header">
<h1 style={style}>Hello React!</h1>
</header>
</div>
);
}
React.FC
사용함수의 매개변수 타입을 지정해주는 거라고 생각하면 됨
interface MyComponentProps {
name: string;
}
const MyComponent: React.FC<MyComponentProps> = ({ name }) => {
return <div>Hello, {name}!</div>;
};