
- 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 - 頭盔元件(Helmet)
Web 文件的元資訊對於 SEO 非常重要。文件的元資訊通常在 head 部分使用 meta 標籤指定。title 標籤在提供文件的元資訊方面也發揮著重要作用。Head 部分還將包含 script 和 style 標籤。Helmet 元件透過提供所有有效的 head 標籤,提供了一種簡單的方法來管理文件的 head 部分。Helmet 將收集其內部指定的所有資訊,並更新文件的 head 部分。
讓我們在本節學習如何在 React 中使用 Helmet 元件。
安裝 Helmet
在學習 Helmet 的概念和用法之前,讓我們先學習如何使用 npm 命令安裝 Helmet 元件。
npm install --save react-helmet
上述命令將安裝 Helmet 元件,並使其在我們的應用程式中可以使用。
Helmet 的概念和用法
Helmet 接受所有有效的 head 標籤。它接受普通的 HTML 標籤,並將標籤輸出到文件的 head 部分,如下所示:
import React from "react"; import {Helmet} from "react-helmet"; class Application extends React.Component { render () { return ( <div className="application"> <Helmet> <title>Helmet sample application</title> <meta charSet="utf-8" /> <meta name="description" content="Helmet sample program to understand the working of the helmet component" /> <meta name="keywords" content="React, Helmet, HTML, CSS, Javascript" /> <meta name="author" content="Peter" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </Helmet> // ... </div> ); } };
這裡:
title 用於指定文件的標題
description meta 標籤用於指定文件的詳細資訊
keywords 用於指定文件的主要關鍵詞。搜尋引擎將使用它。
author 用於指定文件的作者
viewport 用於指定文件的預設視口
charSet 用於指定文件中使用的字元集。
head 部分的輸出如下:
<head> <title>Helmet sample application</title> <meta charSet="utf-8" /> <meta name="description" content="Helmet sample program to understand the working of the helmet component" /> <meta name="keywords" content="React, Helmet, HTML, CSS, Javascript" /> <meta name="author" content="Peter" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </head>
Helmet 元件可以在任何其他 React 元件內部使用以更改 header 部分。它也可以巢狀,以便在渲染子元件時更改 header 部分。
<Parent> <Helmet> <title>Helmet sample application</title> <meta charSet="utf-8" /> <meta name="description" content="Helmet sample program to understand the working of the helmet component" /> <meta name="keywords" content="React, Helmet, HTML, CSS, Javascript" /> <meta name="author" content="Peter" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </Helmet> <Child> <Helmet> <title>Helmet sample application :: rendered by child component</title> <meta name="description" content="Helmet sample program to explain nested feature of the helmet component" /> </Helmet> </Child> </Parent>
這裡:
子元件中的 Helmet 將更新 head 部分,如下所示:
<head> <title>Helmet sample application :: rendered by child component</title> <meta charSet="utf-8" /> <meta name="description" content="Helmet sample program to explain nested feature of the helmet component" /> <meta name="keywords" content="React, Helmet, HTML, CSS, Javascript"> <meta name="author" content="Peter"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head>
應用 Helmet
讓我們在本節建立一個新的 React 應用程式,學習如何在其中應用 Helmet 元件。
首先,使用以下命令建立一個新的 React 應用程式並啟動它。
create-react-app myapp cd myapp npm start
接下來,開啟 App.css (src/App.css) 並移除所有 CSS 類。
// remove all css classes
接下來,建立一個簡單的元件,SimpleHelmet (src/Components/SimpleHelmet.js) 並渲染:
import React from "react"; import {Helmet} from "react-helmet"; class SimpleHelmet extends React.Component { render() { return ( <div> <Helmet> <title>Helmet sample application</title> <meta charSet="utf-8" /> <meta name="description" content="Helmet sample program to understand the working of the helmet component" /> <meta name="keywords" content="React, Helmet, HTML, CSS, Javascript" /> <meta name="author" content="Peter" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> </Helmet> <p>A sample application to demonstrate helmet component.</p> </div> ) } } export default SimpleHelmet;
這裡我們有:
從 react-helmet 包中匯入了 Helmet
在元件中使用 Helmet 更新 head 部分。
接下來,開啟 App 元件 (src/App.js),並使用 SimpleHelmet 元件,如下所示:
import './App.css' import React from 'react'; import SimpleHelmet from './Components/SimpleHelmet' function App() { return ( <div className="container"> <div style={{ padding: "10px" }}> <div> <SimpleHelmet /> </div> </div> </div> ); } export default App;
這裡我們有:
從 react 包中匯入了 SimpleHelmet 元件
使用了 SimpleHelmet 元件
接下來,開啟 index.html (public/index.html) 檔案並移除 meta 標籤,如下所示:
<!DOCTYPE html> <html lang="en"> <head> <link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> <link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" /> <link rel="manifest" href="%PUBLIC_URL%/manifest.json" /> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div style="padding: 10px;"> <div id="root"></div> </div> </body> </html>
這裡:
title 標籤被移除
meta 標籤(描述、主題顏色和視口)被移除
最後,在瀏覽器中開啟應用程式並檢查最終結果。

在開發者工具中開啟原始碼,您可以看到如下所示的 html 資訊:
<title>Helmet sample application</title> <meta charset="utf-8" data-react-helmet="true"> <meta name="description" content="Helmet sample program to understand the working of the helmet component" data-react-helmet="true"> <meta name="keywords" content="React, Helmet, HTML, CSS, Javascript" data-react-helmet="true"> <meta name="author" content="Peter" data-react-helmet="true"> <meta name="viewport" content="width=device-width, initial-scale=1.0" data-react-helmet="true">
總結
Helmet 元件是一個易於使用的元件,用於管理文件的 head 內容,支援伺服器端和客戶端渲染。