React ์ต์์ API
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:
React๋ React ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ์ง์
์ ์
๋๋ค. <script> ํ๊ทธ๋ฅผ ์ฌ์ฉํ์ฌ React๋ฅผ ๋ถ๋ฌ์ค๊ฒ ๋๋ฉด ์ ์ญ ๊ฐ์ฒด React๋ฅผ ํตํ์ฌ ์ต์์ API๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. npm์์ ES6๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, import React from 'react'๋ฅผ ์์ฑํ๋ฉด ๋ฉ๋๋ค. npm์์ ES5๋ฅผ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ, var React = require('react')๋ฅผ ์์ฑํ๋ฉด ๋ฉ๋๋ค.
๊ฐ์
์ปดํฌ๋ํธ
React ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ฉด UI๋ฅผ ๋
๋ฆฝ์ ์ด๊ณ ์ฌ์ฌ์ฉํ ์ ์๋ ๋ถ๋ถ์ผ๋ก ๋๋๊ณ ๊ฐ ๋ถ๋ถ์ ๋ถ๋ฆฌํ์ฌ ์๊ฐํ ์ ์์ต๋๋ค. React ์ปดํฌ๋ํธ๋ React.Component ๋๋ React.PureComponent๋ก ์ธ๋ถ์ ์ผ๋ก ๋๋์ด ์ ์ํ ์ ์์ต๋๋ค.
ES6 class๋ฅผ ์ฌ์ฉํ์ง ์๋๋ค๋ฉด, create-react-class ๋ชจ๋์ ๋์ ์ฌ์ฉํด๋ ๋ฉ๋๋ค. ์์ธํ ์ ๋ณด๋ ES6 ์์ด React๋ฅผ ์ฌ์ฉํ๊ธฐ ๋ฌธ์์์ ํ์ธํ ์ ์์ต๋๋ค.
React ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ ๋ ๋ํ๋ ์ ์๋ ํจ์์ ํํ๋ก ํ ์๋ ์์ต๋๋ค.
React ์๋ฆฌ๋จผํธ ์์ฑํ๊ธฐ
UI์ ํํ๋ฅผ ์ค๋ช
ํ๋ ๋ฐ์ JSX๋ฅผ ์ฌ์ฉํ ๊ฒ์ ๊ถ์ฅํฉ๋๋ค. ๊ฐ JSX ์๋ฆฌ๋จผํธ๋ ๋จ์ง React.createElement()๋ฅผ ํธ์ถํ๋ ํธ๋ฆฌํ ๋ฌธ๋ฒ์ ๋ถ๊ณผํฉ๋๋ค. JSX๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ์๋์ ๋ฉ์๋๋ค์ ์ง์ ํธ์ถํ๋ ์ผ์ ๊ฑฐ์ ์์ต๋๋ค.
์์ธํ ์ ๋ณด๋ JSX ์์ด React ์ฌ์ฉํ๊ธฐ ๋ฌธ์์์ ํ์ธํ ์ ์์ต๋๋ค.
์๋ฆฌ๋จผํธ ๋ณํํ๊ธฐ
React๋ ์๋ฆฌ๋จผํธ๋ฅผ ์กฐ์ํ๋ API๋ค์ ์ ๊ณตํฉ๋๋ค.
Fragments
๋ํ React๋ ๋ํผ ์์ด ์ฌ๋ฌ ์๋ฆฌ๋จผํธ๋ฅผ ๋ ๋๋งํ ์ ์๋ ์ปดํฌ๋ํธ๋ฅผ ์ ๊ณตํฉ๋๋ค.
Refs
Suspense
Suspense๋ฅผ ์ฌ์ฉํ๋ฉด ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋งํ๊ธฐ ์ ์ ๋ค๋ฅธ ์์
์ด ๋จผ์ ์ด๋ฃจ์ด์ง๋๋ก โ๋๊ธฐํฉ๋๋คโ. ํ์ฌ Suspense๋ ๋จ ํ๋์ ์ฌ์ฉ ์ฌ๋ก React.lazy๋ฅผ ์ฌ์ฉํ์ฌ ์ปดํฌ๋ํธ๋ฅผ ๋์ ์ผ๋ก ๋ถ๋ฌ์ค๊ธฐ๋ง ์ง์ํฉ๋๋ค. ๋์ค์๋ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ์ ๊ฐ์ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ์ง์ํ ๊ณํ์
๋๋ค.
Transitions
Transitions๋ React 18์ ๋์ ๋ ์๋ก์ด ๋์(concurrent) ๊ธฐ๋ฅ์ ๋๋ค. ์ด๋ ์ ๋ฐ์ดํธ๋ฅผ transitions์ผ๋ก ํ์ํ ์ ์๊ฒ ํ์ฌ React์๊ฒ ์ค๋จ๋ ์ ์์์ ์๋ฆฌ๊ณ , ์ด๋ฏธ ๋ณด์ด๋ ์ฝํ ์ธ ์ ๋ํด Suspense Fallback์ผ๋ก ๊ฐ๋ ๊ฒ์ ๋ฐฉ์งํ ์ ์์ต๋๋ค.
Hooks
Hooks๋ React 16.8์ ์๋ก ์ถ๊ฐ๋์์ต๋๋ค. Hooks๋ฅผ ์ฌ์ฉํ๋ฉด class๋ฅผ ์ฌ์ฉํ์ง ์์๋ state์ React ๊ธฐ๋ฅ๋ค์ ์ฌ์ฉํ ์ ์๋๋ก ํด์ค๋๋ค. Hooks๋ง์ ๋ค๋ฃจ๋ ๋ฌธ์์ API ๋ฌธ์๊ฐ ์กด์ฌํฉ๋๋ค.
Reference
React.Component
This content is out of date.
Read the new React documentation for
Component.
React.Component๋ ES6 class๋ฅผ ์ฌ์ฉํ์ฌ React ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ ๋์ ๊ธฐ์ด๊ฐ ๋๋ class์
๋๋ค.
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}</h1>;
}
}React.Component ๊ธฐ์ด class์ ๊ด๋ จ๋ ๋ฉ์๋์ ์์ฑ ๋ชฉ๋ก์ React.Component API Reference์์ ํ์ธํ ์ ์์ต๋๋ค.
React.PureComponent
This content is out of date.
Read the new React documentation for
PureComponent.
React.PureComponent๋ React.Component์ ๋น์ทํฉ๋๋ค. React.Component๋ shouldComponentUpdate()๋ฅผ ๊ตฌํํ์ง ์์ง๋ง, React.PureComponent๋ props์ state๋ฅผ ์ด์ฉํ ์์ ๋น๊ต๋ฅผ ๊ตฌํํ๋ค๋ ์ฐจ์ด์ ๋ง์ด ์กด์ฌํฉ๋๋ค.
React ์ปดํฌ๋ํธ์ render() ํจ์๊ฐ ๋์ผํ props์ state์ ๋ํ์ฌ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ ๋๋งํ๋ค๋ฉด, React.PureComponent๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ์ฑ๋ฅ ํฅ์์ ๋๋ฆด ์ ์์ต๋๋ค.
์ฃผ์
React.PureComponent์shouldComponentUpdate()๋ ์ปดํฌ๋ํธ์ ๋ํ์ฌ ์์ ๋น๊ต๋ง์ ์ํํฉ๋๋ค. ๋ฐ๋ผ์ ์ปดํฌ๋ํธ์ ๋ณต์กํ ์๋ฃ ๊ตฌ์กฐ๊ฐ ํฌํจ๋์ด์๋ค๋ฉด, ๊น์ ์ฐจ์ด๊ฐ ์กด์ฌํจ์๋ ๋ถ๊ตฌํ๊ณ ์ฐจ์ด๊ฐ ์๋ค๊ณ ํ๋จํ๋ ์๋ชป๋ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด๋ผ ์ ์์ต๋๋ค. props์ state์ ๊ตฌ์กฐ๊ฐ ๊ฐ๋จํ ๊ฒ์ผ๋ก ์์๋ ๋์๋งPureComponent๋ฅผ ์์ํ๊ณ , ๊น์ ์๋ฃ ๊ตฌ์กฐ์ ๋ณํ๊ฐ ์๋ค๋ฉดforceUpdate()๋ฅผ ์ฌ์ฉํ์ธ์. ๋๋ ์ค์ฒฉ๋ ๋ฐ์ดํฐ๋ค์ ๋น ๋ฅด๊ฒ ๋น๊ตํ ์ ์๋๋ก ํ๋ ค๋ฉด ๋ถ๋ณ ๊ฐ์ฒด์ ์ฌ์ฉ์ ๊ฒํ ํด๋ณด์ธ์.๋ ๋์๊ฐ
React.PureComponent์shouldComponentUpdate()๋ ์ปดํฌ๋ํธ์ ํ์ ํธ๋ฆฌ์ ๋ํ props ๊ฐฑ์ ์์ ์ ์ํํ์ง ์์ต๋๋ค. ์์ ์ปดํฌ๋ํธ๋ค์ด โ์์โํ์ง ๊ผญ ํ์ธํ๊ธฐ ๋ฐ๋๋๋ค.
React.memo
This content is out of date.
Read the new React documentation for
memo.
const MyComponent = React.memo(function MyComponent(props) {
/* props๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋๋ง */
});React.memo๋ ๊ณ ์ฐจ ์ปดํฌ๋ํธ(Higher Order Component)์
๋๋ค.
์ปดํฌ๋ํธ๊ฐ ๋์ผํ props๋ก ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ๋ ๋๋งํด๋ธ๋ค๋ฉด, React.memo๋ฅผ ํธ์ถํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ฉ๋ชจ์ด์ง(Memoizing)ํ๋๋ก ๋ํํ์ฌ ๊ฒฝ์ฐ์ ๋ฐ๋ผ ์ฑ๋ฅ ํฅ์์ ๋๋ฆด ์ ์์ต๋๋ค. ์ฆ, React๋ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ์ง ์๊ณ ๋ง์ง๋ง์ผ๋ก ๋ ๋๋ง๋ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ฌ์ฉํฉ๋๋ค.
React.memo๋ props ๋ณํ์๋ง ์ํฅ์ ์ค๋๋ค. React.memo๋ก ๊ฐ์ธ์ง ํจ์ ์ปดํฌ๋ํธ ๊ตฌํ์ useState, useReducer ๋๋ useContext ํ
์ ์ฌ์ฉํ๋ค๋ฉด, ์ฌ์ ํ state๋ context๊ฐ ๋ณํ ๋ ๋ค์ ๋ ๋๋ง๋ฉ๋๋ค.
props๊ฐ ๊ฐ๋ ๋ณต์กํ ๊ฐ์ฒด์ ๋ํ์ฌ ์์ ๋น๊ต๋ง์ ์ํํ๋ ๊ฒ์ด ๊ธฐ๋ณธ ๋์์ ๋๋ค. ๋ค๋ฅธ ๋น๊ต ๋์์ ์ํ๋ค๋ฉด, ๋ ๋ฒ์งธ ์ธ์๋ก ๋ณ๋์ ๋น๊ต ํจ์๋ฅผ ์ ๊ณตํ๋ฉด ๋ฉ๋๋ค.
function MyComponent(props) {
/* props๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๋๋ง */
}
function areEqual(prevProps, nextProps) {
/*
nextProps๊ฐ prevProps์ ๋์ผํ ๊ฐ์ ๊ฐ์ง๋ฉด true๋ฅผ ๋ฐํํ๊ณ , ๊ทธ๋ ์ง ์๋ค๋ฉด false๋ฅผ ๋ฐํ
*/
}
export default React.memo(MyComponent, areEqual);์ด ๋ฉ์๋๋ ์ค์ง ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํ์ฌ ์ฌ์ฉ๋ฉ๋๋ค. ๋ ๋๋ง์ โ๋ฐฉ์งโํ๊ธฐ ์ํ์ฌ ์ฌ์ฉํ์ง ๋ง์ธ์. ๋ฒ๊ทธ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค.
์ฃผ์
class ์ปดํฌ๋ํธ์
shouldComponentUpdate()๋ฉ์๋์ ๋ฌ๋ฆฌ,areEqualํจ์๋ props๋ค์ด ์๋ก ๊ฐ์ผ๋ฉดtrue๋ฅผ ๋ฐํํ๊ณ , props๋ค์ด ์๋ก ๋ค๋ฅด๋ฉดfalse๋ฅผ ๋ฐํํฉ๋๋ค. ์ด๊ฒ์shouldComponentUpdate์ ์ ๋ฐ๋์ ๋์์ ๋๋ค.
createElement()
This content is out of date.
Read the new React documentation for
createElement.
React.createElement(
type,
[props],
[...children]
)์ธ์๋ก ์ฃผ์ด์ง๋ ํ์
์ ๋ฐ๋ผ ์๋ก์ด React ์๋ฆฌ๋จผํธ๋ฅผ ์์ฑํ์ฌ ๋ฐํํฉ๋๋ค. type ์ธ์๋ก๋ ํ๊ทธ ์ด๋ฆ ๋ฌธ์์ด('div' ๋๋ 'span' ๋ฑ), React ์ปดํฌ๋ํธ ํ์
, ๋๋ React Fragment ํ์
์ค ํ๋๊ฐ ์ฌ ์ ์์ต๋๋ค.
JSX๋ก ์์ฑ๋ ์ฝ๋๋ React.createElement()๋ฅผ ์ฌ์ฉํ๋ ํํ๋ก ๋ณํ๋ฉ๋๋ค. JSX๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ React.createElement()๋ฅผ ์ง์ ํธ์ถํ๋ ์ผ์ ๊ฑฐ์ ์์ต๋๋ค. ์์ธํ ์ ๋ณด๋ JSX ์์ด React ์ฌ์ฉํ๊ธฐ ๋ฌธ์์์ ํ์ธํ ์ ์์ต๋๋ค.
cloneElement()
This content is out of date.
Read the new React documentation for
cloneElement.
React.cloneElement(
element,
[config],
[...children]
)element๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ก์ด React ์๋ฆฌ๋จผํธ๋ฅผ ๋ณต์ฌํ๊ณ ๋ฐํํฉ๋๋ค. config๋ key์ ref ๊ทธ๋ฆฌ๊ณ ๋ชจ๋ ์๋ก์ด props๋ฅผ ํฌํจํฉ๋๋ค. ์๋ก์ด ์๋ฆฌ๋จผํธ์๋ ์๋ณธ ์๋ฆฌ๋จผํธ๊ฐ ๊ฐ์ก๋ props๊ฐ ์๋ก์ด props์ ์๊ฒ ํฉ์ณ์ง ๋ค ์ฃผ์ด์ง๋๋ค. ์๋ก์ด ์์๋ค์ ๊ธฐ์กด์ ์์๋ค์ ๋์ฒดํฉ๋๋ค. config์ key์ ref๊ฐ ์๋ค๋ฉด ์๋ณธ ์๋ฆฌ๋จผํธ์ key์ ref๋ ๊ทธ๋๋ก ์ ์ง๋ฉ๋๋ค.
React.cloneElement()๋ ์๋์ ๊ตฌ๋ฌธ๊ณผ ๊ฑฐ์ ๋๋ฑํฉ๋๋ค.
<element.type {...element.props} {...props}>{children}</element.type>๊ทธ๋ ์ง๋ง ref๋ค์ด ์ ์ง๋๋ค๋ ์ ์ด ๋ค๋ฆ
๋๋ค. ์ฆ ์กฐ์์ด ๊ฐ์ง๊ณ ์์ ref๋ฅผ ์ฌ์ฉํ์ฌ ์์ ์๋ฆฌ๋จผํธ์ ์ ๊ทผํ๋ ๊ฒ์ด ํ์ฉ๋ฉ๋๋ค. ์๋ก์ด ์๋ฆฌ๋จผํธ์ ๋ง๋ถ์ฌ์ง๋ ๊ฒ๊ณผ ๋์ผํ ref๋ฅผ ์ป์ ์ ์์ต๋๋ค. ์๋ก์ด ref ๋๋ key๊ฐ ์๋ค๋ฉด ์ด์ ๊ฐ์ ๋์ฒดํฉ๋๋ค.
์ด API๋ ๋ ์ด์ ์ฌ์ฉ๋์ง ์๋ React.addons.cloneWithProps()๋ฅผ ๋์ฒดํฉ๋๋ค.
createFactory()
This content is out of date.
Read the new React documentation for
createFactory.
React.createFactory(type)์ฃผ์ด์ง ํ์
์ React ์๋ฆฌ๋จผํธ๋ฅผ ๋ง๋ค์ด๋ด๋ ํจ์๋ฅผ ๋ฐํํฉ๋๋ค. React.createElement()์ ๋ง์ฐฌ๊ฐ์ง๋ก type ์ธ์๋ ํ๊ทธ ์ด๋ฆ ๋ฌธ์์ด('div' ๋๋ 'span' ๋ฑ), React ์ปดํฌ๋ํธ ํ์
, ๋๋ React Fragment ํ์
์ค ํ๋๊ฐ ์ฌ ์ ์์ต๋๋ค.
์ด ํฌํผ ํจ์๋ ๋ ๊ฑฐ์ ๊ธฐ๋ฅ์ผ๋ก ๊ฐ์ฃผ๋๋ฉฐ, ๋์ JSX ๋ฌธ๋ฒ์ ์ฌ์ฉํ๊ฑฐ๋ React.createElement()๋ฅผ ์ง์ ์ฌ์ฉํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
JSX๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ React.createFactory()๋ฅผ ์ง์ ํธ์ถํ๋ ์ผ์ ๊ฑฐ์ ์์ต๋๋ค. ์์ธํ ์ ๋ณด๋ JSX ์์ด React ์ฌ์ฉํ๊ธฐ ๋ฌธ์์์ ํ์ธํ ์ ์์ต๋๋ค.
isValidElement()
This content is out of date.
Read the new React documentation for
isValidElement.
React.isValidElement(object)๊ฐ์ฒด๊ฐ React ์๋ฆฌ๋จผํธ์ธ์ง ํ์ธํฉ๋๋ค. true ๋๋ false๋ฅผ ๋ฐํํฉ๋๋ค.
React.Children
This content is out of date.
Read the new React documentation for
Children.
React.Children๋ ๋ถํฌ๋ช
(Opaque) ์๋ฃ ๊ตฌ์กฐ์ธ this.props.children๋ฅผ ๋ค๋ฃจ๋ ์ ํธ๋ฆฌํฐ ํจ์๋ค์ ์ ๊ณตํฉ๋๋ค.
React.Children.map
React.Children.map(children, function[(thisArg)])children์ ํฌํจ๋ ๊ฐ ์์์ ๋ํ์ฌ this๋ฅผ thisArg์ ๊ฐ์ผ๋ก ์ค์ ํ ํจ์๋ฅผ ํธ์ถํฉ๋๋ค. children์ด ๋ฐฐ์ด์ผ ๊ฒฝ์ฐ, ์ด ๋ฐฐ์ด์ ๊ฐ ์์์ ๋ํ์ฌ ํจ์๊ฐ ํธ์ถ๋ฉ๋๋ค. children์ด null ๋๋ undefined์ผ ๊ฒฝ์ฐ, ์ด ๋ฉ์๋๋ ๋ฐฐ์ด์ด ์๋๋ผ null ๋๋ undefined๋ฅผ ๋ฐํํฉ๋๋ค.
์ฃผ์
children์ดFragment์ผ ๊ฒฝ์ฐ,children์ ๋จ์ผ ์์์ผ๋ก ์ทจ๊ธ๋์ด ์ํํ์ง ์์ต๋๋ค.
React.Children.forEach
React.Children.forEach(children, function[(thisArg)])React.Children.map()์ ๋น์ทํ์ง๋ง, ๋ฐฐ์ด์ ๋ฐํํ์ง ์์ต๋๋ค.
React.Children.count
React.Children.count(children)children์ ํฌํจ๋ ์ปดํฌ๋ํธ์ ๊ฐ์๋ฅผ ๋ฐํํฉ๋๋ค. map ๋๋ forEach๋ก ์ ๋ฌ๋ ์ฝ๋ฐฑ์ด ํธ์ถ๋ ํ์์ ๋์ผํ ๊ฐ์
๋๋ค.
React.Children.only
React.Children.only(children)children์ด ๋จ ํ๋์ ์์(React ์๋ฆฌ๋จผํธ)๋ฅผ ๊ฐ๋์ง ํ์ธํ๊ณ ํด๋น ์์ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค. ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ ์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค.
์ฃผ์
React.Children.only()๋React.Children.map()์ ๋ฐํ๊ฐ์ ํ์ฉํ์ง ์๋๋ฐ, ์๋ํ๋ฉด ๋ฐํ๊ฐ์ด React ์๋ฆฌ๋จผํธ๊ฐ ์๋๋ผ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ ๋๋ค.
React.Children.toArray
React.Children.toArray(children)๊ฐ ์์์ key๊ฐ ํ ๋น๋ ๋ฐฐ์ด์ children ๋ถํฌ๋ช
(opaque) ์๋ฃ ๊ตฌ์กฐ๋ก ๋ฐํํฉ๋๋ค. render() ๋ฉ์๋์์ children์ ์งํฉ์ ๋ค๋ฃจ๊ณ ์ถ์ ๋, ํนํ this.props.children์ ํ๋ถ๋ก ์ ๋ฌํ๊ธฐ ์ ์ ๋ค์ ์ ๋ ฌํ๊ฑฐ๋ ์ผ๋ถ๋ง ์๋ผ๋ด๊ณ ์ถ์ ๋์ ์ ์ฉํฉ๋๋ค.
์ฃผ์
React.Children.toArray()๋children์ ํํํ๊ฒ(Flatten) ๋ง๋ค ๋, ์ค์ฒฉ๋ ๋ฐฐ์ด๋ค์ ์๋ฏธ๋ฅผ ๋ณด์กดํ๊ธฐ ์ํ์ฌkey๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค. ์ฆ,toArray๋ ๋ฐํ๋๋ ๋ฐฐ์ด์key๊ฐ์ ๋ง๋ถ์ฌ์ ๊ฐ ์๋ฆฌ๋จผํธ๊ฐ ๊ฐ๋key๊ฐ ํํํด์ง ๋ฐฐ์ด ๋ด์์๋ง ์ ํจํ ๋ฒ์๋ฅผ ํ์ฑํ๋๋ก ํด์ค๋๋ค.
React.Fragment
This content is out of date.
Read the new React documentation for
Fragment.
React.Fragment ์ปดํฌ๋ํธ๋ฅผ ์ฌ์ฉํ๋ฉด render() ๋ฉ์๋ ์์์ ์ถ๊ฐ์ ์ธ DOM ์๋ฆฌ๋จผํธ๋ฅผ ์์ฑํ์ง ์์๋ ์ฌ๋ฌ ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํ ์ ์์ต๋๋ค.
render() {
return (
<React.Fragment>
Some text.
<h2>A heading</h2>
</React.Fragment>
);
}์ถ์ฝํ์ธ <></> ๋ฌธ๋ฒ์ผ๋ก๋ ๋์ผํ๊ฒ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์์ธํ ์ ๋ณด๋ React v16.2.0: Fragment์ ๋ํ ํฅ์๋ ์ง์์์ ํ์ธํ ์ ์์ต๋๋ค.
React.createRef
This content is out of date.
Read the new React documentation for
createRef.
React.createRef๋ React ์๋ฆฌ๋จผํธ์ ref ์ดํธ๋ฆฌ๋ทฐํธ๋ก ๋ถ์ผ ์ ์๋ ref๋ฅผ ์์ฑํฉ๋๋ค.
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef(); }
render() {
return <input type="text" ref={this.inputRef} />; }
componentDidMount() {
this.inputRef.current.focus(); }
}
React.forwardRef
This content is out of date.
Read the new React documentation for
forwardRef.
React.forwardRef๋ ์ ๋ฌ๋ฐ์ ref ์ดํธ๋ฆฌ๋ทฐํธ๋ฅผ ํ๋ถ ํธ๋ฆฌ ๋ด์ ๋ค๋ฅธ ์ปดํฌ๋ํธ๋ก ์ ๋ฌํ๋ React ์ปดํฌ๋ํธ๋ฅผ ์์ฑํฉ๋๋ค. ์ด ๊ธฐ๋ฒ์ ์ ์ฌ์ฉ๋์ง ์์ง๋ง, ์๋์ ๋ ์๋๋ฆฌ์ค์์๋ ํนํ ์ ์ฉํฉ๋๋ค.
- DOM ์๋ฆฌ๋จผํธ๋ก ref ์ ๋ฌํ๊ธฐ
- ๊ณ ์ฐจ ์ปดํฌ๋ํธ(Higher Order Component)๋ก ref ์ ๋ฌํ๊ธฐ
React.forwardRef๋ ๋ ๋๋ง์ ์ฌ์ฉ๋ ํจ์๋ฅผ ์ธ์๋ก ๋ฐ์ ์ ์์ต๋๋ค. React๋ ์ด ํจ์๋ฅผ ๋ ๊ฐ ์ธ์ props์ ref๋ฅผ ์ฌ์ฉํ์ฌ ํธ์ถํ๊ณ , ์ด ํจ์๋ React ๋
ธ๋๋ฅผ ๋ฐํํฉ๋๋ค.
const FancyButton = React.forwardRef((props, ref) => ( <button ref={ref} className="FancyButton"> {props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef();
<FancyButton ref={ref}>Click me!</FancyButton>;
์์ ์์์์ React๋ <FancyButton ref={ref}> ์๋ฆฌ๋จผํธ์ ์ฃผ์ด์ง ref๋ฅผ React.forwardRef ํธ์ถ์ ๋ ๋๋ง ํจ์์ 2๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํฉ๋๋ค. ์ด ๋ ๋๋ง ํจ์๋ ref๋ฅผ <button ref={ref}> ์๋ฆฌ๋จผํธ์ ์ ๋ฌํฉ๋๋ค.
๋ฐ๋ผ์ React๊ฐ ํด๋น ref๋ฅผ ๋ถ์ด๊ณ ๋ ๋ค, ref.current๋ <button> DOM ์๋ฆฌ๋จผํธ ์ธ์คํด์ค๋ฅผ ์ง์ ๊ฐ๋ฆฌํค๊ฒ ๋ฉ๋๋ค.
์์ธํ ์ ๋ณด๋ ref ์ ๋ฌํ๊ธฐ์์ ํ์ธํ ์ ์์ต๋๋ค.
React.lazy
This content is out of date.
Read the new React documentation for
lazy.
React.lazy()๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ ์ผ๋ก ๋ถ๋ฌ์ค๋ ์ปดํฌ๋ํธ๋ฅผ ์ ์ํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ฉด ๋ฒ๋ค์ ํฌ๊ธฐ๋ฅผ ์ค์ด๊ณ , ์ด๊ธฐ ๋ ๋๋ง์์ ์ฌ์ฉ๋์ง ์๋ ์ปดํฌ๋ํธ๋ฅผ ๋ถ๋ฌ์ค๋ ์์
์ ์ง์ฐ์ํฌ ์ ์์ต๋๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ์ Code Splitting ๋ฌธ์์์ ์ตํ ์ ์์ต๋๋ค. ๋ํ ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์์ธํ ๋ค๋ฃฌ ์ด ๊ธ์ ์ฝ์ด๋ณด๋ ๊ฒ๋ ์ข์ต๋๋ค.
// ์ด ์ปดํฌ๋ํธ๋ ๋์ ์ผ๋ก ๋ถ๋ฌ์ต๋๋ค
const SomeComponent = React.lazy(() => import('./SomeComponent'));lazyํ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋๋งํ๋ ค๋ฉด ๋ ๋๋ง ํธ๋ฆฌ ์์์ <React.Suspense> ์ปดํฌ๋ํธ๊ฐ ์กด์ฌํด์ผ ํ๋ค๋ ์ ์ ์ ์ํ์ธ์. ์ด๋ฅผ ํ์ฉํ์ฌ ๋ก๋ฉ ์ง์๊ธฐ(Loading indicator)๋ฅผ ๋ํ๋ผ ์ ์์ต๋๋ค.
React.Suspense
This content is out of date.
Read the new React documentation for
Suspense.
React.Suspense lets you specify the loading indicator in case some components in the tree below it are not yet ready to render. In the future we plan to let Suspense handle more scenarios such as data fetching. You can read about this in our roadmap.
React.Suspense๋ ํธ๋ฆฌ ํ๋จ์ ์๋ ์ผ๋ถ ์ปดํฌ๋ํธ๊ฐ ์์ง ๋ ๋๋งํ ์ค๋น๊ฐ ๋์ง ์์ ๊ฒฝ์ฐ ๋ก๋ฉ ์ง์๊ธฐ (Loading indicator)๋ฅผ ๋ํ๋ผ ์ ์๋๋ก ํด์ค๋๋ค. ํฅํ์๋ Suspense๊ฐ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ(fetching)๋ฑ์ ๋ ๋ง์ ์๋๋ฆฌ์ค๋ฅผ ์ฒ๋ฆฌํ ์ ์๋๋ก ํ ๊ณํ์
๋๋ค. ์ด์ ๋ํ ๋ด์ฉ์ ๋ก๋๋งต์์ ํ์ธํ ์ ์์ต๋๋ค.
ํ์ฌ <React.Suspense>๊ฐ ์ง์ํ๋ ์ ์ผํ ์ฌ์ฉ ์ฌ๋ก๋ lazy loading ์ปดํฌ๋ํธ์
๋๋ค.
// ์ด ์ปดํฌ๋ํธ๋ ๋์ ์ผ๋ก ๋ถ๋ฌ์ต๋๋ค
const OtherComponent = React.lazy(() => import('./OtherComponent'));
function MyComponent() {
return (
// Displays <Spinner> until OtherComponent loads
<React.Suspense fallback={<Spinner />}>
<div>
<OtherComponent />
</div>
</React.Suspense>
);
}๊ด๋ จ๋ ๋ด์ฉ์ Code Splitting ๊ฐ์ด๋ ๋ฌธ์์์ ์ค๋ช
ํ๊ณ ์์ต๋๋ค. lazyํ ์ปดํฌ๋ํธ๋ Suspense ํธ๋ฆฌ ๋ด์ ๊น์ํ ๊ณณ์ ์์นํ ์ ์๋ค๋ ์ ์ ์ ์ํ์ธ์. ์ฆ, Suspense๊ฐ ๋ชจ๋ ์ปดํฌ๋ํธ๋ฅผ ๊ฐ์ ํ์๋ ์๋ค๋ ๊ฒ์
๋๋ค. ๊ฐ์ฅ ์ข์ ์ฌ์ฉ๋ฒ์ ๋ก๋ฉ ์ง์๊ธฐ๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์ถ์ ์ง์ ์ <Suspense>๋ฅผ ์์ฑํ๋ ๊ฒ์ด์ง๋ง, Code Splitting์ ํ๊ณ ์ ํ๋ ์ง์ ์ด๋์๋ ์ง lazy()๋ฅผ ์จ์ผ ํ ๊ฒ์
๋๋ค.
์ฃผ์
์ฌ์ฉ์์๊ฒ ์ด๋ฏธ ํ์๋ ์ฝํ ์ธ ์ ๊ฒฝ์ฐ, ๋ค์ ๋ก๋ฉ ์ง์๊ธฐ(Loading Indicator)๋ก ์ ํ๋๋ ๊ฒ์ ํผ๋์ ์ผ๊ธฐํ ์ ์์ต๋๋ค. ๋๋ก๋ ์๋ก์ด UI๊ฐ ์ค๋น๋๋ ๋์ โ๊ธฐ์กด์โ UI๋ฅผ ๋ณด์ฌ์ฃผ๋ ๊ฒ์ด ๋ ๋์ ์ ์์ต๋๋ค. ์ด๋ฅผ ์ํด, ์๋ก์ด transition API์ธ
startTransition๊ณผuseTransition๋ฅผ ์ฌ์ฉํ์ฌ ์์ ๋ ๋ด์ฉ์ transition์ผ๋ก ํ์ํ๊ณ ์์์น ๋ชปํ fallback์ ๋ฐฉ์งํ ์ ์์ต๋๋ค.
์๋ฒ ์ฌ์ด๋ ๋ ๋๋ง์์์ React.Suspense
์๋ฒ ์ฌ์ด๋ ๋ ๋๋ง ์ค์ Suspense ๊ฒฝ๊ณ(Suspense Boundaries)๋ฅผ ์ฌ์ฉํ๋ฉด ์ดํ๋ฆฌ์ผ์ด์ ์ ์ผ์ ์ค๋จํ์ฌ ๋ ์์ ๋ฉ์ด๋ฆฌ๋ก ๋ ๋๋งํ ์ ์์ต๋๋ค. ์ปดํฌ๋ํธ ๋ ๋๋ง์ด ์ผ์ ์ค๋จ๋๋ฉด ๊ฐ์ฅ ๊ฐ๊น์ด Suspense boundary์ fallback์ ๋ ๋๋งํ๊ธฐ ์ํด ์ฐ์ ์์๊ฐ ๋ฎ์ ์์ ์ ์์ฝํฉ๋๋ค. Fallback์ ๋ ๋๋ง ํ๊ธฐ ์ ์ ์ปดํฌ๋ํธ ๋ ๋๋ง์ด ์ฌ๊ฐ๋๋ค๋ฉด(unsuspends) ์ค์ ์ฝํ ์ธ ๋ฅผ ๋ณด์ฌ์ฃผ๊ณ fallback์ ์ ๊ฑฐํฉ๋๋ค.
React.Suspense during hydration
Suspense boundaries depend on their parent boundaries being hydrated before they can hydrate, but they can hydrate independently from sibling boundaries. Events on a boundary before it is hydrated will cause the boundary to hydrate at a higher priority than neighboring boundaries. Read more
React.startTransition
This content is out of date.
Read the new React documentation for
startTransition.
React.startTransition(callback)React.startTransition ๋ ์ ๊ณต๋ ์ฝ๋ฐฑ์ ๋ด๋ถ๋ฅผ transition์ผ๋ก ํ์ํ ์ ์๊ฒํฉ๋๋ค. ํด๋น ๋ฉ์๋๋ React.useTransition ๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋๋ก ์ค๊ณ๋์์ต๋๋ค.
์ฃผ์
Transition ์ ๋ฐ์ดํธ๋ click๊ณผ ๊ฐ์ ์ฐ์ ์์๊ฐ ๋์ ์ ๋ฐ์ดํธ์ ์์๋ฅผ ์๋ณดํฉ๋๋ค.
Transition ์ค ์ผ์ด๋๋ ์ ๋ฐ์ดํธ๋ ๋ค์ ์ผ์ ์ค๋จ๋(re-suspended) ์ฝํ ์ธ ์ ๋ํ fallback์ ํ์ํ์ง ์์ผ๋ฏ๋ก, ์ฌ์ฉ์๊ฐ ์ ๋ฐ์ดํธ๋ฅผ ๋ ๋๋งํ๋ ๋์ ๊ณ์ํ์ฌ ์ํธ ์์ฉ ํ ์ ์๋๋ก ํด์ค๋๋ค.
React.startTransition๋isPendingํ๋๊ทธ๋ฅผ ์ ๊ณตํ์ง ์์ต๋๋ค. Transition์ ๋๊ธฐ ์ํ(pending status)๋ฅผ ์ถ์ ํ๋ ค๋ฉดReact.useTransition๋ฅผ ์ฐธ๊ณ ํ์ธ์.