ReactDOM
The react-dom package provides DOM-specific methods that can be used at the top level of your app and as an escape hatch to get outside the React model if you need to.
import * as ReactDOM from 'react-dom';If you use ES5 with npm, you can write:
var ReactDOM = require('react-dom');The react-dom package also provides modules specific to client and server apps:
๊ฐ์
The react-dom package exports these methods:
These react-dom methods are also exported, but are considered legacy:
Note:
Both
renderandhydratehave been replaced with new client methods in React 18. These methods will warn that your app will behave as if itโs running React 17 (learn more here).
๋ธ๋ผ์ฐ์ ์ง์
React supports all modern browsers, although some polyfills are required for older versions.
์ฃผ์
We do not support older browsers that donโt support ES5 methods or microtasks such as Internet Explorer. You may find that your apps do work in older browsers if polyfills such as es5-shim and es5-sham are included in the page, but youโre on your own if you choose to take this path.
์ฐธ์กฐ
createPortal()
Try the new React documentation for
createPortal.The new docs will soon replace this site, which will be archived. Provide feedback.
createPortal(child, container)Creates a portal. Portals provide a way to render children into a DOM node that exists outside the hierarchy of the DOM component.
flushSync()
Try the new React documentation for
flushSync.The new docs will soon replace this site, which will be archived. Provide feedback.
flushSync(callback)Force React to flush any updates inside the provided callback synchronously. This ensures that the DOM is updated immediately.
// Force this state update to be synchronous.
flushSync(() => {
setCount(count + 1);
});
// By this point, DOM is updated.Note:
flushSynccan significantly hurt performance. Use sparingly.
flushSyncmay force pending Suspense boundaries to show theirfallbackstate.
flushSyncmay also run pending effects and synchronously apply any updates they contain before returning.
flushSyncmay also flush updates outside the callback when necessary to flush the updates inside the callback. For example, if there are pending updates from a click, React may flush those before flushing the updates inside the callback.
Legacy Reference
render()
Try the new React documentation for
render.The new docs will soon replace this site, which will be archived. Provide feedback.
render(element, container[, callback])Note:
renderhas been replaced withcreateRootin React 18. See createRoot for more info.
Render a React element into the DOM in the supplied container and return a reference to the component (or returns null for stateless components).
์ฃผ์
render()๋ ์ ๋ฌํ ์ปจํ ์ด๋ ๋ ธ๋์ ์ฝํ ์ธ ๋ฅผ ์ ์ดํฉ๋๋ค. ์ฒ์ ํธ์ถํ ๋ ๊ธฐ์กด์ DOM ์๋ฆฌ๋จผํธ๋ฅผ ๊ต์ฒดํ๋ฉฐ ์ดํ์ ํธ์ถ์ React์ DOM diffing ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์ฌ ๋์ฑ ํจ์จ์ ์ผ๋ก ์ ๋ฐ์ดํธํฉ๋๋ค.
render()๋ ์ปจํ ์ด๋ ๋ ธ๋๋ฅผ ์์ ํ์ง ์๊ณ ์ปจํ ์ด๋์ ํ์ ๋ ธ๋๋ง ์์ ํฉ๋๋ค. ๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ์์ ๋ ธ๋๋ฅผ ๋ฎ์ด์ธ ํ์ ์์ด ๊ธฐ์กด์ DOM ๋ ธ๋์ ์ปดํฌ๋ํธ๋ฅผ ์ถ๊ฐํ ์ ์์ต๋๋ค.
render()๋ ํ์ฌReactComponent๋ฃจํธ(root) ์ธ์คํด์ค์ ๋ํ ์ฐธ์กฐ๋ฅผ ๋ฐํํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด ๋ฐํ ๊ฐ์ ์ฌ์ฉํ๋ ๊ฒ์ ๋ ๊ฑฐ์์ด๋ฉฐ React ์ ๊ท ๋ฒ์ ์ด ์ปดํฌ๋ํธ๋ฅผ ๋น๋๊ธฐ๋ก ๋ ๋๋งํ๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ๋๋ฌธ์ ํผํด์ผ ํฉ๋๋ค.ReactComponent์ธ์คํด์ค์ ์ฐธ์กฐ๊ฐ ํ์ํ๋ค๋ฉด ๊ถ์ฅํ๋ ํด๊ฒฐ์ฑ ์ ๋ฃจํธ ์๋ฆฌ๋จผํธ์ ์ฝ๋ฐฑ ref๋ฅผ ๋ถ์ด๋ ๊ฒ์ ๋๋ค.
render()๋ฅผ ์ฌ์ฉํด ์๋ฒ์์ ๋ ๋๋งํ ์ปจํ ์ด๋์ ์ด๋ฒคํธ๋ฅผ ๋ณด์ถฉํ๋ ๊ฒ์ ๊ถ์ฅ๋์ง ์์ผ๋ฉฐ React 17 ๋ฒ์ ์์ ์ญ์ ๋ ์์ ์ ๋๋ค.hydrateRoot()๋ฅผ ์ฌ์ฉํด์ฃผ์ธ์.
hydrate()
Try the new React documentation for
hydrate.The new docs will soon replace this site, which will be archived. Provide feedback.
hydrate(element, container[, callback])์ฃผ์
hydrate๋ React 18์์hydrateRoot๋ก ๋์ฒด๋์์ต๋๋ค.ย ์์ธํ ๋ด์ฉ์ hydrateRoot๋ฅผ ์ฐธ์กฐํ์ญ์์ค.
render()์ ๋์ผํ์ง๋ง HTML ์ฝํ
์ธ ๊ฐ ReactDOMServer๋ก ๋ ๋๋ง ๋ ์ปจํ
์ด๋์ ์ด๋ฒคํธ๋ฅผ ๋ณด์ถฉํ๊ธฐ ์ํด ์ฌ์ฉ๋ฉ๋๋ค. React๋ ๊ธฐ์กด ๋งํฌ์
์ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ์ฐ๊ฒฐํฉ๋๋ค.
React๋ ๋ ๋๋ง ๋ ์ฝํ ์ธ ๊ฐ ์๋ฒ์ ํด๋ผ์ด์ธํธ ๊ฐ์ ๊ฐ์ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค. React๊ฐ ํ ์คํธ ์ฝํ ์ธ ์ ์ฐจ์ด๋ฅผ ๊ณ ์น ์๋ ์์ง๋ง ์ด๋ฌํ ๋ถ์ผ์น๋ฅผ ๋ฒ๊ทธ๋ก ์ทจ๊ธํ์ฌ ๊ณ ์ณ์ผ ํฉ๋๋ค. ๊ฐ๋ฐ ๋ชจ๋์์ React๋ ์ด๋ฒคํธ ๋ณด์ถฉ ์ค ๋ฐ์ํ๋ ๋ถ์ผ์น์ ๋ํด ๊ฒฝ๊ณ ํฉ๋๋ค. ๋ถ์ผ์น๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ์ ์ดํธ๋ฆฌ๋ทฐํธ ์ฐจ์ด๋ฅผ ๊ณ ์น๋ค๋ ๋ณด์ฅ์ด ์์ต๋๋ค. ๋๋ค์์ ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ถ์ผ์น๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ๋ ๋ง์ง ์์ผ๋ฉฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ๋ชจ๋ ๋งํฌ์ ์ ๊ฒ์ฆํ๋ ๊ฒ์ด ๋งค์ฐ ํฐ ๋น์ฉ์ ์๋ฐํ๊ธฐ ๋๋ฌธ์ ์ฑ๋ฅ์์ ์ด์ ๋ก ์ค์ํ ๋ฌธ์ ์ ๋๋ค.
์๋ฒ์ ํด๋ผ์ด์ธํธ ์ฌ์ด์์ ๋จ์ผ ์๋ฆฌ๋จผํธ์ ์ดํธ๋ฆฌ๋ทฐํธ๋ ํ
์คํธ๊ฐ ๋ถ๊ฐํผํ๊ฒ ๋ค๋ฅด๋ค๋ฉด(์๋ฅผ ๋ค์ด timestamp์ ๊ฒฝ์ฐ) ๊ทธ ์๋ฆฌ๋จผํธ์ suppressHydrationWarning={true}๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ผ๋ก ๊ฒฝ๊ณ ๋ฅผ ๋ ์ ์์ต๋๋ค. ์ด๋ ํ ๋จ๊ณ๊น์ง๋ง ์๋ํ๋ฉฐ ์๋๋ ํด๊ฒฐ์ฑ
์
๋๋ค. ์ ๋ ๋จ์ฉํ์ง ๋ง์ธ์. ํ
์คํธ๊ฐ ์๋๋ผ๋ฉด React๋ ํด๋น ์๋ฆฌ๋จผํธ๋ฅผ ๊ณ ์น์ง ์์ ๊ฒ์ด๋ฉฐ ์ดํ์ ์
๋ฐ์ดํธ๊น์ง ์ผ์นํ์ง ์์ ์ฑ๋ก ๋จ์์์ ๊ฒ์
๋๋ค.
์๋ฒ์ ํด๋ผ์ด์ธํธ ๊ฐ์ ์ฐจ์ด๋ฅผ ์๋ํ๋ค๋ฉด 2๋จ๊ณ ๋ ๋๋ง์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ํด๋ผ์ด์ธํธ์์ ๋ค๋ฅด๊ฒ ๋ ๋๋ง ๋๋ ์ปดํฌ๋ํธ๋ componentDidMount()์์ true๋ก ์ค์ ํ ์ ์๋ this.state.isClient์ ๊ฐ์ ์ํ ๋ณ์๋ฅผ ์ฝ์ ์ ์์ต๋๋ค. ์ด ๋ฐฉ์์ผ๋ก ์ด๊ธฐ ๋ ๋ ๋จ๊ณ๋ ์๋ฒ์ ๊ฐ์ ์ฝํ
์ธ ๋ฅผ ๋ ๋๋งํ์ฌ ๋ถ์ผ์น๋ฅผ ๋ฐฉ์งํ์ง๋ง, ์ด๋ฒคํธ ๋ณด์ถฉ ์งํ์ ์ถ๊ฐ์ ์ธ ๋จ๊ณ๊ฐ ๋๊ธฐ์ ์ผ๋ก ๋ฐ์ํฉ๋๋ค. ์ด ๋ฐฉ์์ ์ปดํฌ๋ํธ๋ฅผ ๋ ๋ฒ ๋ ๋๋งํ๊ฒ ๋ง๋ค์ด ์๋๋ฅผ ๋๋ฆฌ๊ฒ ํ ์ ์๊ธฐ ๋๋ฌธ์ ์ฃผ์๋ฅผ ๊ธฐ์ธ์ฌ์ผ ํฉ๋๋ค.
๋๋ฆฐ ์ฐ๊ฒฐ์์์ ์ฌ์ฉ์ ๊ฒฝํ์ ์ ์ํด์ผ ํฉ๋๋ค. JavaScript ์ฝ๋๋ ์ต์ด HTML ๋ ๋๋ง๋ณด๋ค ๋งค์ฐ ๋ฆ๊ฒ ๋ก๋๋ ์ ์์ผ๋ฉฐ ํด๋ผ์ด์ธํธ ์ ์ฉ ๋จ๊ณ์์ ๋ค๋ฅธ ๋ฌด์ธ๊ฐ๋ฅผ ๋ ๋๋งํ๋ค๋ฉด ๊ทธ ์ ํ ๊ณผ์ ์์ ๋ฐฉํด๋ฅผ ๋ฐ์ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ ์์ ์ผ๋ก ์คํ๋๋ค๋ฉด ์๋ฒ์ ์ ํ๋ฆฌ์ผ์ด์ โshellโ์ ๋ ๋๋งํ๊ณ ํด๋ผ์ด์ธํธ์์ ์ผ๋ถ ์ถ๊ฐ ์์ ฏ๋ง ํ์ํ๋ ๊ฒ์ด ํจ๊ณผ์ ์ผ ์ ์์ต๋๋ค. ๋งํฌ์ ๋ถ์ผ์น ๋ฌธ์ ์์ด ์ด ๋ฐฉ์์ ์ฌ์ฉํ๊ธธ ์ํ๋ค๋ฉด ์ด์ ๋จ๋ฝ์ ์ค๋ช ์ ์ฐธ๊ณ ํด์ฃผ์ธ์.
unmountComponentAtNode()
Try the new React documentation for
unmountComponentAtNode.The new docs will soon replace this site, which will be archived. Provide feedback.
unmountComponentAtNode(container)Note:
unmountComponentAtNodehas been replaced withroot.unmount()in React 18. See createRoot for more info.
๋ง์ดํธ๋ React ์ปดํฌ๋ํธ๋ฅผ DOM์์ ์ ๊ฑฐํ๊ณ ์ปดํฌ๋ํธ์ ์ด๋ฒคํธ ํธ๋ค๋ฌ์ state๋ฅผ ์ ๋ฆฌํฉ๋๋ค. ์ปจํ
์ด๋์ ์๋ฌด๋ฐ ์ปดํฌ๋ํธ๋ ๋ง์ดํธ ๋์ง ์์๋ค๋ฉด ํด๋น ํจ์๋ฅผ ํธ์ถํ๋๋ผ๋ ์๋ฌด๋ฐ ๋์์ ํ์ง ์์ต๋๋ค. ์ปดํฌ๋ํธ๊ฐ ๋ง์ดํธ ํด์ ๋์๋ค๋ฉด true, ๋ง์ดํธ ํด์ ํ ์ปดํฌ๋ํธ๊ฐ ์กด์ฌํ์ง ์๋๋ค๋ฉด false๋ฅผ ๋ฐํํฉ๋๋ค.
findDOMNode()
Try the new React documentation for
findDOMNode.The new docs will soon replace this site, which will be archived. Provide feedback.
Note:
findDOMNode๋ ๊ธฐ๋ณธ DOM ๋ ธ๋๋ฅผ ์ด์ฉํ๋ ๋ฐ์ ์ฌ์ฉ๋๋ ํด๊ฒฐ์ฑ ์ ๋๋ค. ๋๋ถ๋ถ์ ๊ฒฝ์ฐ์์ ์ด ํด๊ฒฐ์ฑ ์ ์ฌ์ฉํ๋ ๊ฒ์ ์ปดํฌ๋ํธ ์ถ์ํ๋ฅผ ์๋ฐํ๊ธฐ ๋๋ฌธ์ ๊ถ์ฅํ์ง ์์ต๋๋ค. ์ด ๋ฉ์๋๋StrictMode์์ ๊ถ์ฅ๋์ง ์์ต๋๋ค.
findDOMNode(component)๋งค๊ฐ ๋ณ์๋ก ์ ๋ฌ๋ฐ์ ์ปดํฌ๋ํธ๊ฐ DOM ๋ด๋ถ์ ๋ง์ดํธ๋์๋ค๋ฉด ์ปดํฌ๋ํธ์ ํด๋นํ๋ ๋ค์ดํฐ๋ธ ๋ธ๋ผ์ฐ์ ์ DOM ์๋ฆฌ๋จผํธ๋ฅผ ๋ฐํํฉ๋๋ค. ํด๋น ๋ฉ์๋๋ form ํ๋ ๊ฐ์ด๋ DOM ์ฑ๋ฅ ์ธก์ ๊ณผ ๊ฐ์ด DOM์์ ๊ฐ์ ์ฝ์ ๋ ์ ์ฉํฉ๋๋ค. ๋๋ถ๋ถ์ ๊ฒฝ์ฐ์์ DOM ๋
ธ๋์ ref๋ฅผ ๋ถ์ผ ์ ์์ผ๋ฉฐ findDOMNode๋ฅผ ์ฌ์ฉํ ํ์๊ฐ ์ ํ ์์ต๋๋ค.
์ปดํฌ๋ํธ๊ฐ null์ด๋ false๋ฅผ ๋ ๋๋งํ๋ ๊ฒฝ์ฐ findDOMNode์ null์ ๋ฐํํฉ๋๋ค. ์ปดํฌ๋ํธ๊ฐ ๋ฌธ์๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ findDOMNode์ ๋ฌธ์ ๊ฐ์ ํฌํจํ๊ณ ์๋ ํ
์คํธ DOM ๋
ธ๋๋ฅผ ๋ฐํํฉ๋๋ค. React 16๋ถํฐ ์ปดํฌ๋ํธ๋ ์ฌ๋ฌ ๊ฐ์ ์์์ ๊ฐ์ง fragment๋ฅผ ๋ฐํํ ์ ์์ผ๋ฉฐ ์ด ๊ฒฝ์ฐ์ findDOMNode๋ ๋น์ด์์ง ์์ ์ฒซ ๋ฒ์งธ ์์์ ํด๋นํ๋ DOM ๋
ธ๋๋ฅผ ๋ฐํํฉ๋๋ค.
์ฃผ์
findDOMNode๋ ๋ง์ดํธ๋ ์ปดํฌ๋ํธ์๋ง ๋์ํฉ๋๋ค(์ฆ ์ปดํฌ๋ํธ๋ DOM ๋ด๋ถ์ ์กด์ฌํด์ผ ํฉ๋๋ค). ์์ง ๋ง์ดํธ๋์ง ์์ ์ปดํฌ๋ํธ๋ฅผ ๋์์ผ๋ก ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค๋ฉด(์๋ฅผ ๋ค์ด ์์ง ์์ฑ๋์ง ์์ ์ปดํฌ๋ํธ์render()์์findDOMNode()๋ฅผ ํธ์ถํ๋ ๊ฒ) exception์ด ๋ฐ์ํฉ๋๋ค.
findDOMNode์ ํจ์ ์ปดํฌ๋ํธ์์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.