์ฝ๋ ๋ถํ
These docs are old and wonโt be updated. Go to react.dev for the new React docs.
These new documentation pages teach modern React and include live examples:
๋ฒ๋ค๋ง
๋๋ถ๋ถ React ์ฑ๋ค์ Webpack, Rollup ๋๋ Browserify ๊ฐ์ ํด์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ํ์ผ์ ํ๋๋ก ๋ณํฉํ โ๋ฒ๋ค ๋โ ํ์ผ์ ์น ํ์ด์ง์ ํฌํจํ์ฌ ํ ๋ฒ์ ์ ์ฒด ์ฑ์ ๋ก๋ ํ ์ ์์ต๋๋ค.
์์
App
// app.js
import { add } from './math.js';
console.log(add(16, 26)); // 42
// math.js
export function add(a, b) {
return a + b;
}
Bundle
function add(a, b) {
return a + b;
}
console.log(add(16, 26)); // 42
์ฃผ์
์ค์ ๋ฒ๋ค์ ์ ์์์๋ ๋ง์ด ๋ค๋ฅด๊ฒ ๋ณด์ผ ๊ฒ๋๋ค.
Create React App์ด๋ Next.js, Gatsby ํน์ ๋น์ทํ ํด์ ์ฌ์ฉํ๋ค๋ฉด ์ฌ๋ฌ๋ถ์ด ์ค์นํ ์ฑ์์ Webpack์ ๊ฐ์ด ์ค์นํ์ ๊ฒ๋๋ค.
์ด๋ฐ ํด์ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด ์ฌ๋ฌ๋ถ์ด ์ค์ค๋ก ๋ฒ๋ค๋ง์ ์ค์ ํด์ผ ํฉ๋๋ค. ์ด ๊ฒฝ์ฐ Webpack์ ์ค์นํ๊ธฐ ๋ฌธ์์ ์์ํ๊ธฐ ๋ฌธ์๋ฅผ ์ฐธ์กฐํด ์ฃผ์ธ์.
์ฝ๋ ๋ถํ
๋ฒ๋ค๋ง์ ํ๋ฅญํ์ง๋ง ์ฌ๋ฌ๋ถ์ ์ฑ์ด ์ปค์ง๋ฉด ๋ฒ๋ค๋ ์ปค์ง๋๋ค. ํนํ ํฐ ๊ท๋ชจ์ ์๋ ํํฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ ๋ ์ค์๋ก ์ฑ์ด ์ปค์ ธ์ ๋ก๋ ์๊ฐ์ด ๊ธธ์ด์ง๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํด ์ฝ๋๋ฅผ ์ฃผ์ ๊น๊ฒ ์ดํด์ผ ํฉ๋๋ค.
๋ฒ๋ค์ด ๊ฑฐ๋ํด์ง๋ ๊ฒ์ ๋ฐฉ์งํ๊ธฐ ์ํ ์ข์ ํด๊ฒฐ๋ฐฉ๋ฒ์ ๋ฒ๋ค์ โ๋๋๋โ ๊ฒ์ ๋๋ค. ์ฝ๋ ๋ถํ ์ ๋ฐํ์์ ์ฌ๋ฌ ๋ฒ๋ค์ ๋์ ์ผ๋ก ๋ง๋ค๊ณ ๋ถ๋ฌ์ค๋ ๊ฒ์ผ๋ก Webpack, Rollup๊ณผ Browserify (factor-bundle) ๊ฐ์ ๋ฒ๋ค๋ฌ๊ฐ ์ง์ํ๋ ๊ธฐ๋ฅ์ ๋๋ค.
์ฝ๋ ๋ถํ ์ ์ฌ๋ฌ๋ถ์ ์ฑ์ โ์ง์ฐ ๋ก๋ฉโ ํ๊ฒ ๋์์ฃผ๊ณ ์ฑ ์ฌ์ฉ์์๊ฒ ํ๊ธฐ์ ์ธ ์ฑ๋ฅ ํฅ์์ ํ๊ฒ ํฉ๋๋ค. ์ฑ์ ์ฝ๋ ์์ ์ค์ด์ง ์๊ณ ๋ ์ฌ์ฉ์๊ฐ ํ์ํ์ง ์์ ์ฝ๋๋ฅผ ๋ถ๋ฌ์ค์ง ์๊ฒ ํ๋ฉฐ ์ฑ์ ์ด๊ธฐํ ๋ก๋ฉ์ ํ์ํ ๋น์ฉ์ ์ค์ฌ์ค๋๋ค.
import()
์ฑ์ ์ฝ๋ ๋ถํ ์ ๋์
ํ๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ ๋์ import()
๋ฌธ๋ฒ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์
๋๋ค.
Before
import { add } from './math';
console.log(add(16, 26));
After
import("./math").then(math => {
console.log(math.add(16, 26));
});
Webpack์ด ์ด ๊ตฌ๋ฌธ์ ๋ง๋๊ฒ ๋๋ฉด ์ฑ์ ์ฝ๋๋ฅผ ๋ถํ ํฉ๋๋ค. Create React App์ ์ฌ์ฉํ๊ณ ์๋ค๋ฉด ์ด๋ฏธ Webpack์ด ๊ตฌ์ฑ์ด ๋์ด ์๊ธฐ ๋๋ฌธ์ ์ฆ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. Next.js ์ญ์ ์ง์ํฉ๋๋ค.
์ฝ๋ ๋ถํ ๊ฐ์ด๋๋ฅผ ์ฐธ์กฐํ์ธ์. Webpack ์ค์ ์ ๊ฐ์ด๋์ ์์ต๋๋ค.
Babel์ ์ฌ์ฉํ ๋๋ Babel์ด ๋์ import๋ฅผ ์ธ์ํ ์ ์์ง๋ง ๋ณํํ์ง๋ ์๋๋ก ํฉ๋๋ค. ์ด๋ฅผ ์ํด @babel/plugin-syntax-dynamic-import๋ฅผ ์ฌ์ฉํ์ธ์.
React.lazy
React.lazy
ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ import๋ฅผ ์ฌ์ฉํด์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋ง ํ ์ ์์ต๋๋ค.
Before
import OtherComponent from './OtherComponent';
After
const OtherComponent = React.lazy(() => import('./OtherComponent'));
MyComponent
๊ฐ ์ฒ์ ๋ ๋๋ง ๋ ๋ OtherComponent
๋ฅผ ํฌํจํ ๋ฒ๋ค์ ์๋์ผ๋ก ๋ถ๋ฌ์ต๋๋ค.
React.lazy
๋ ๋์ import()
๋ฅผ ํธ์ถํ๋ ํจ์๋ฅผ ์ธ์๋ก ๊ฐ์ง๋๋ค. ์ด ํจ์๋ React ์ปดํฌ๋ํธ๋ฅผ default
export๋ก ๊ฐ์ง ๋ชจ๋ ๊ฐ์ฒด๊ฐ ์ดํ๋๋ Promise
๋ฅผ ๋ฐํํด์ผ ํฉ๋๋ค.
lazy ์ปดํฌ๋ํธ๋ Suspense
์ปดํฌ๋ํธ ํ์์์ ๋ ๋๋ง๋์ด์ผ ํ๋ฉฐ, Suspense
๋ lazy ์ปดํฌ๋ํธ๊ฐ ๋ก๋๋๊ธธ ๊ธฐ๋ค๋ฆฌ๋ ๋์ ๋ก๋ฉ ํ๋ฉด๊ณผ ๊ฐ์ ์๋น ์ปจํ
์ธ ๋ฅผ ๋ณด์ฌ์ค ์ ์๊ฒ ํด์ค๋๋ค.
import React, { Suspense } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<OtherComponent />
</Suspense>
</div>
);
}
fallback
prop์ ์ปดํฌ๋ํธ๊ฐ ๋ก๋๋ ๋๊น์ง ๊ธฐ๋ค๋ฆฌ๋ ๋์ ๋ ๋๋งํ๋ ค๋ React ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐ์๋ค์
๋๋ค. Suspense
์ปดํฌ๋ํธ๋ lazy ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์๋๋ค. ํ๋์ Suspense
์ปดํฌ๋ํธ๋ก ์ฌ๋ฌ lazy ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ ์๋ ์์ต๋๋ค.
import React, { Suspense } from 'react';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
function MyComponent() {
return (
<div>
<Suspense fallback={<div>Loading...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
</section>
</Suspense>
</div>
);
}
Avoiding fallbacks
Any component may suspend as a result of rendering, even components that were already shown to the user. In order for screen content to always be consistent, if an already shown component suspends, React has to hide its tree up to the closest <Suspense>
boundary. However, from the userโs perspective, this can be disorienting.
Consider this tab switcher:
import React, { Suspense } from 'react';
import Tabs from './Tabs';
import Glimmer from './Glimmer';
const Comments = React.lazy(() => import('./Comments'));
const Photos = React.lazy(() => import('./Photos'));
function MyComponent() {
const [tab, setTab] = React.useState('photos');
function handleTabSelect(tab) {
setTab(tab);
};
return (
<div>
<Tabs onTabSelect={handleTabSelect} />
<Suspense fallback={<Glimmer />}>
{tab === 'photos' ? <Photos /> : <Comments />}
</Suspense>
</div>
);
}
In this example, if tab gets changed from 'photos'
to 'comments'
, but Comments
suspends, the user will see a glimmer. This makes sense because the user no longer wants to see Photos
, the Comments
component is not ready to render anything, and React needs to keep the user experience consistent, so it has no choice but to show the Glimmer
above.
However, sometimes this user experience is not desirable. In particular, it is sometimes better to show the โoldโ UI while the new UI is being prepared. You can use the new startTransition
API to make React do this:
function handleTabSelect(tab) {
startTransition(() => {
setTab(tab);
});
}
Here, you tell React that setting tab to 'comments'
is not an urgent update, but is a transition that may take some time. React will then keep the old UI in place and interactive, and will switch to showing <Comments />
when it is ready. See Transitions for more info.
Error boundaries
๋คํธ์ํฌ ์ฅ์ ๊ฐ์ ์ด์ ๋ก ๋ค๋ฅธ ๋ชจ๋์ ๋ก๋์ ์คํจํ ๊ฒฝ์ฐ ์๋ฌ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค. ์ด๋ Error Boundaries๋ฅผ ์ด์ฉํ์ฌ ์ฌ์ฉ์์ ๊ฒฝํ๊ณผ ๋ณต๊ตฌ ๊ด๋ฆฌ๋ฅผ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค. Error Boundary๋ฅผ ๋ง๋ค๊ณ lazy ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ธ๋ฉด ๋คํธ์ํฌ ์ฅ์ ๊ฐ ๋ฐ์ํ์ ๋ ์๋ฌ๋ฅผ ํ์ํ ์ ์์ต๋๋ค.
import React, { Suspense } from 'react';
import MyErrorBoundary from './MyErrorBoundary';
const OtherComponent = React.lazy(() => import('./OtherComponent'));
const AnotherComponent = React.lazy(() => import('./AnotherComponent'));
const MyComponent = () => (
<div>
<MyErrorBoundary>
<Suspense fallback={<div>Loading...</div>}>
<section>
<OtherComponent />
<AnotherComponent />
</section>
</Suspense>
</MyErrorBoundary>
</div>
);
Route-based code splitting
์ฑ์ ์ฝ๋ ๋ถํ ์ ์ด๋ ๊ณณ์ ๋์ ํ ์ง ๊ฒฐ์ ํ๋ ๊ฒ์ ์กฐ๊ธ ๊น๋ค๋กญ์ต๋๋ค. ์ฌ๋ฌ๋ถ์ ์ฌ์ฉ์์ ๊ฒฝํ์ ํด์น์ง ์์ผ๋ฉด์ ๋ฒ๋ค์ ๊ท ๋ฑํ๊ฒ ๋ถ๋ฐฐํ ๊ณณ์ ์ฐพ๊ณ ์ ํฉ๋๋ค.
์ด๋ฅผ ์์ํ๊ธฐ ์ข์ ์ฅ์๋ ๋ผ์ฐํธ์ ๋๋ค. ์น ํ์ด์ง๋ฅผ ๋ถ๋ฌ์ค๋ ์๊ฐ์ ํ์ด์ง ์ ํ์ ์ด๋ ์ ๋ ๋ฐ์ํ๋ฉฐ ๋๋ถ๋ถ ํ์ด์ง๋ฅผ ํ๋ฒ์ ๋ ๋๋งํ๊ธฐ ๋๋ฌธ์ ์ฌ์ฉ์๊ฐ ํ์ด์ง๋ฅผ ๋ ๋๋งํ๋ ๋์ ๋ค๋ฅธ ์์์ ์ํธ์์ฉํ์ง ์์ต๋๋ค.
React.lazy
๋ฅผ React Router ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ์ ํ๋ฆฌ์ผ์ด์
์ ๋ผ์ฐํธ ๊ธฐ๋ฐ ์ฝ๋ ๋ถํ ์ ์ค์ ํ๋ ์์์
๋๋ค.
import React, { Suspense, lazy } from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
const Home = lazy(() => import('./routes/Home'));
const About = lazy(() => import('./routes/About'));
const App = () => (
<Router>
<Suspense fallback={<div>Loading...</div>}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Suspense>
</Router>
);
Named Exports
React.lazy
๋ ํ์ฌ default exports๋ง ์ง์ํฉ๋๋ค. named exports๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ๋ค๋ฉด default๋ก ์ด๋ฆ์ ์ฌ์ ์ํ ์ค๊ฐ ๋ชจ๋์ ์์ฑํ ์ ์์ต๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด tree shaking์ด ๊ณ์ ๋์ํ๊ณ ์ฌ์ฉํ์ง ์๋ ์ปดํฌ๋ํธ๋ ๊ฐ์ ธ์ค์ง ์์ต๋๋ค.
// ManyComponents.js
export const MyComponent = /* ... */;
export const MyUnusedComponent = /* ... */;
// MyComponent.js
export { MyComponent as default } from "./ManyComponents.js";
// MyApp.js
import React, { lazy } from 'react';
const MyComponent = lazy(() => import("./MyComponent.js"));