We want to hear from you!Take our 2021 Community Survey!
Support Ukraine ๐Ÿ‡บ๐Ÿ‡ฆ Help Provide Humanitarian Aid to Ukraine.

ReactDOMServer

ReactDOMServer ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ •์  ๋งˆํฌ์—…์œผ๋กœ ๋ Œ๋”๋งํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋Œ€์ฒด๋กœ ์ด๊ฒƒ์€ Node ์„œ๋ฒ„์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

// ES modules
import ReactDOMServer from 'react-dom/server';
// CommonJS
var ReactDOMServer = require('react-dom/server');

๊ฐœ์š”

๋‹ค์Œ ๋ฉ”์„œ๋“œ๋Š” ์„œ๋ฒ„์™€ ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ์ถ”๊ฐ€ ๋ฉ”์„œ๋“œ๋Š” ์„œ๋ฒ„์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” stream ํŒจํ‚ค์ง€์— ์˜์กด์„ฑ์ด ์žˆ์–ด ๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.


์ฐธ์กฐ

renderToString()

ReactDOMServer.renderToString(element)

React ์—˜๋ฆฌ๋จผํŠธ์˜ ์ดˆ๊ธฐ HTML์„ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค. React๋Š” HTML ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋น ๋ฅธ ํŽ˜์ด์ง€ ๋กœ๋“œ๋ฅผ ์œ„ํ•ด ์ดˆ๊ธฐ ์š”์ฒญ ์‹œ์— ์„œ๋ฒ„์—์„œ HTML์„ ์ƒ์„ฑํ•˜์—ฌ ๋งˆํฌ์—…์„ ๋ณด๋‚ด๊ฑฐ๋‚˜, ๊ฒ€์ƒ‰ ์—”์ง„ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด ๊ฒ€์ƒ‰ ์—”์ง„์ด ํŽ˜์ด์ง€๋ฅผ ํฌ๋กค๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฏธ ์„œ๋ฒ„ ๋ Œ๋”๋ง ๋œ ๋งˆํฌ์—…์ด ์žˆ๋Š” ๋…ธ๋“œ์—์„œ ReactDOM.hydrate()๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ React๋Š” ์ด๋ฅผ ๋ณด์กดํ•˜๊ณ  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋งŒ ์—ฐ๊ฒฐํ•จ์œผ๋กœ์จ ๋งค์šฐ ๋›ฐ์–ด๋‚œ ์ฒซ ๋กœ๋“œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.


renderToStaticMarkup()

ReactDOMServer.renderToStaticMarkup(element)

renderToString๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ data-reactroot์™€ ๊ฐ™์ด React์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ DOM ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๋ถ„์˜ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ์•ฝ๊ฐ„์˜ ๋ฐ”์ดํŠธ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ React๋ฅผ ๊ฐ„๋‹จํ•œ ์ •์  ํŽ˜์ด์ง€ ์ƒ์„ฑ๊ธฐ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ์—์„œ React๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•œ ๋งˆํฌ์—…์„ ๋งŒ๋“ค๋ ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. ๋Œ€์‹  ์„œ๋ฒ„์—์„œ renderToString์„ ์‚ฌ์šฉํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์—์„œ ReactDOM.hydrate()๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.


renderToNodeStream()

ReactDOMServer.renderToNodeStream(element)

React ์—˜๋ฆฌ๋จผํŠธ์˜ ์ดˆ๊ธฐ HTML์„ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค. HTML ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•˜๋Š” Readable ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ŠคํŠธ๋ฆผ์ด ์ถœ๋ ฅํ•˜๋Š” HTML ๋ฌธ์ž์—ด์€ ReactDOMServer.renderToString์ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค. ๋น ๋ฅธ ํŽ˜์ด์ง€ ๋กœ๋“œ๋ฅผ ์œ„ํ•ด ์ดˆ๊ธฐ ์š”์ฒญ ์‹œ์— ์„œ๋ฒ„์—์„œ HTML์„ ์ƒ์„ฑํ•˜์—ฌ ๋งˆํฌ์—…์„ ๋ณด๋‚ด๊ฑฐ๋‚˜, ๊ฒ€์ƒ‰ ์—”์ง„ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•ด ๊ฒ€์ƒ‰ ์—”์ง„์ด ํŽ˜์ด์ง€๋ฅผ ํฌ๋กค๋งํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋ฏธ ์„œ๋ฒ„ ๋ Œ๋”๋ง ๋œ ๋งˆํฌ์—…์ด ์žˆ๋Š” ๋…ธ๋“œ์—์„œ ReactDOM.hydrate()๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ React๋Š” ์ด๋ฅผ ๋ณด์กดํ•˜๊ณ  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋งŒ ์—ฐ๊ฒฐํ•จ์œผ๋กœ์จ ๋งค์šฐ ๋›ฐ์–ด๋‚œ ์ฒซ ๋กœ๋“œ ์„ฑ๋Šฅ์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์ฃผ์˜

์„œ๋ฒ„์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด API๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜๋˜๋Š” ์ŠคํŠธ๋ฆผ์€ utf-8๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ ์ŠคํŠธ๋ฆผ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ, ํ…์ŠคํŠธ ํŠธ๋žœ์Šค์ฝ”๋”ฉ์„ ์œ„ํ•œ Transform ์ŠคํŠธ๋ฆผ์„ ์ œ๊ณตํ•˜๋Š” iconv-lite์™€ ๊ฐ™์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”.


renderToStaticNodeStream()

ReactDOMServer.renderToStaticNodeStream(element)

renderToNodeStream๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ data-reactroot์™€ ๊ฐ™์ด React์—์„œ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์ถ”๊ฐ€์ ์ธ DOM ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์—ฌ๋ถ„์˜ ์–ดํŠธ๋ฆฌ๋ทฐํŠธ๋ฅผ ์ œ๊ฑฐํ•จ์œผ๋กœ์จ ์•ฝ๊ฐ„์˜ ๋ฐ”์ดํŠธ๋ฅผ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ React๋ฅผ ๊ฐ„๋‹จํ•œ ์ •์  ํŽ˜์ด์ง€ ์ƒ์„ฑ๊ธฐ๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์€ ๊ฒฝ์šฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด ์ŠคํŠธ๋ฆผ์ด ์ถœ๋ ฅํ•˜๋Š” HTML ๋ฌธ์ž์—ด์€ ReactDOMServer.renderToStaticMarkup์ด ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฐ’๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•ฉ๋‹ˆ๋‹ค.

ํด๋ผ์ด์–ธํŠธ์—์„œ React๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒํ˜ธ์ž‘์šฉ์ด ๊ฐ€๋Šฅํ•œ ๋งˆํฌ์—…์„ ๋งŒ๋“ค๋ ค๋ฉด ์ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋งˆ์„ธ์š”. ๋Œ€์‹  ์„œ๋ฒ„์—์„œ renderToNodeStream์„ ์‚ฌ์šฉํ•˜๊ณ  ํด๋ผ์ด์–ธํŠธ์—์„œ ReactDOM.hydrate()๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”.

์ฃผ์˜

์„œ๋ฒ„์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด API๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜๋˜๋Š” ์ŠคํŠธ๋ฆผ์€ utf-8๋กœ ์ธ์ฝ”๋”ฉ๋œ ๋ฐ”์ดํŠธ ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์ฝ”๋”ฉ๋œ ์ŠคํŠธ๋ฆผ์ด ํ•„์š”ํ•  ๊ฒฝ์šฐ, ํ…์ŠคํŠธ ํŠธ๋žœ์Šค์ฝ”๋”ฉ์„ ์œ„ํ•œ Transform ์ŠคํŠธ๋ฆผ์„ ์ œ๊ณตํ•˜๋Š” iconv-lite์™€ ๊ฐ™์€ ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ดํŽด๋ณด์„ธ์š”.

Is this page useful?Edit this page