
- ReactJS 教程
- ReactJS - 首頁
- ReactJS - 簡介
- ReactJS - 路線圖
- ReactJS - 安裝
- ReactJS - 特性
- ReactJS - 優點與缺點
- ReactJS - 架構
- ReactJS - 建立 React 應用
- ReactJS - JSX
- ReactJS - 元件
- ReactJS - 巢狀元件
- ReactJS - 使用新建立的元件
- ReactJS - 元件集合
- ReactJS - 樣式
- ReactJS - 屬性 (props)
- ReactJS - 使用屬性建立元件
- ReactJS - props 驗證
- ReactJS - 建構函式
- ReactJS - 元件生命週期
- ReactJS - 事件管理
- ReactJS - 建立一個事件感知元件
- ReactJS - 在 Expense Manager 應用中引入事件
- ReactJS - 狀態管理
- ReactJS - 狀態管理 API
- ReactJS - 無狀態元件
- ReactJS - 使用 React Hooks 進行狀態管理
- ReactJS - 使用 React Hooks 進行元件生命週期管理
- ReactJS - 佈局元件
- ReactJS - 分頁
- ReactJS - Material UI
- ReactJS - Http 客戶端程式設計
- ReactJS - 表單程式設計
- ReactJS - 受控元件
- ReactJS - 非受控元件
- ReactJS - Formik
- ReactJS - 條件渲染
- ReactJS - 列表
- ReactJS - Keys
- ReactJS - 路由
- ReactJS - Redux
- ReactJS - 動畫
- ReactJS - Bootstrap
- ReactJS - 地圖
- ReactJS - 表格
- ReactJS - 使用 Flux 管理狀態
- ReactJS - 測試
- ReactJS - CLI 命令
- ReactJS - 構建與部署
- ReactJS - 示例
- Hooks
- ReactJS - Hooks 簡介
- ReactJS - 使用 useState
- ReactJS - 使用 useEffect
- ReactJS - 使用 useContext
- ReactJS - 使用 useRef
- ReactJS - 使用 useReducer
- ReactJS - 使用 useCallback
- ReactJS - 使用 useMemo
- ReactJS - 自定義 Hooks
- ReactJS 高階
- ReactJS - 可訪問性
- ReactJS - 程式碼分割
- ReactJS - 上下文
- ReactJS - 錯誤邊界
- ReactJS - 轉發 Refs
- ReactJS - 片段
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 效能最佳化
- ReactJS - Profiler API
- ReactJS - 門戶
- ReactJS - 無 ES6 ECMAScript 的 React
- ReactJS - 無 JSX 的 React
- ReactJS - 調和
- ReactJS - Refs 和 DOM
- ReactJS - 渲染 Props
- ReactJS - 靜態型別檢查
- ReactJS - 嚴格模式
- ReactJS - Web Components
- 其他概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 走馬燈
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考 API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS - renderToStaticMarkup() 方法
React 可以用於客戶端和伺服器端渲染。因此,我們將瞭解 renderToStaticMarkup 函式,它是 React 伺服器端渲染功能的一部分。
什麼是 renderToStaticMarkup?
React 的 renderToStaticMarkup 函式允許我們將非互動式 React 元件轉換為 HTML 字串。換句話說,我們可以說它將我們的 React 元件轉換為純 HTML。當使用 React 生成靜態 HTML 頁面或電子郵件時,這尤其有用。
語法
const html = renderToStaticMarkup(reactNode)
引數
reactNode - 它是要渲染為 HTML 的 React 節點。例如,考慮 JSX 節點 <MyComponent />。
返回值
它返回一個 HTML 字串。
示例
示例 - 基本元件渲染
此應用程式定義了一個名為 App 的簡單 React 元件,它呈現一個帶有問候訊息的基本 div 元素。然後使用 renderToStaticMarkup() 函式將 React 元件轉換為靜態 HTML 標記。程式碼如下:
import React from 'react'; import ReactDOMServer from 'react-dom/server'; const App = () => { return <div>Hello, this is a basic React component!</div>; }; export default App; const staticMarkup = ReactDOMServer.renderToStaticMarkup(<App />); console.log(staticMarkup);
輸出

示例 - 使用 Props
在此應用程式中,我們將有兩個元件 - Greeting 和 App。Greeting 元件接受一個 name prop 並將其合併到問候訊息中。App 元件使用特定名稱(在本例中為 Joseph)呈現 Greeting 元件。因此,程式碼如下所示:
import React from 'react'; import ReactDOMServer from 'react-dom/server'; const Greeting = (props) => { return <div>Hello, {props.name}!</div>; }; const App = () => { return <Greeting name="Joseph" />; }; export default App; const staticMarkup = ReactDOMServer.renderToStaticMarkup(<App />); console.log(staticMarkup);
輸出

示例 - 巢狀元件
此應用程式將具有巢狀元件。有兩個元件 - InnerComponent 和 OuterComponent。OuterComponent 呈現一個 h2 標題並在其中包含 InnerComponent。此應用程式的程式碼如下所示:
import React from 'react'; import ReactDOMServer from 'react-dom/server'; const InnerComponent = () => { return <p>This is an inner component.</p>; }; const OuterComponent = () => { return ( <div> <h2>Hello from the outer component!</h2> <InnerComponent /> </div> ); }; const App = () => { return <OuterComponent />; }; export default App; const staticMarkup = ReactDOMServer.renderToStaticMarkup(<App />); console.log(staticMarkup);
輸出

示例
使用 renderToStaticMarkup 是一種非常簡單的方法。
匯入函式
要使用 renderToStaticMarkup,我們需要透過從 'react-dom/server' 包中匯入它來將其包含在我們的伺服器端程式碼中。
import { renderToStaticMarkup } from 'react-dom/server';
渲染 React 元件
匯入後,我們可以使用該函式渲染我們的 React 元件。例如,假設我們有一個名為 <MyComponent /> 的元件,我們希望將其渲染為 HTML。
const html = renderToStaticMarkup(<MyComponent />);
傳送 HTML 響應
使用 renderToStaticMarkup 後,我們將獲得一個 HTML 字串。然後,我們的伺服器可以將此 HTML 作為響應傳送。在伺服器路由處理程式中如何執行此操作的示例如下:
app.use('/', (request, response) => { const html = renderToStaticMarkup(<MyComponent />); response.send(html); });
為什麼要使用 renderToStaticMarkup()?
當我們需要生成非互動式 HTML 內容時,renderToStaticMarkup 就會發揮作用。它在以下情況下很有幫助:
當我們想要預渲染網頁為靜態 HTML,而無需依賴客戶端 JavaScript 才能工作時。這可以幫助提高 SEO 和頁面載入速度。
為非互動式電子郵件建立 HTML 模板。React 的元件結構可以使電子郵件模板構建更容易。
總結
當我們需要生成非互動式 HTML 內容時,renderToStaticMarkup 是 React 中伺服器端渲染的有用方法。對於靜態網站建立和電子郵件模板建立等任務,它是一個有用的工具,可以新增到我們的工具箱中。