
- 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 APP 中引入事件
- 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 - Web Components
React 和 Web Components 可以混合在 Web 應用程式中。React 元件可以包含一個或多個 Web Components,而 Web Components 可以使用 React 元件來建立其內容。React 支援這兩種選項。
在 React 應用程式中使用 Web Components
讓我們建立一個 Web Components,然後嘗試將其應用於 React 應用程式。首先,建立一個新的 React 應用程式並使用以下命令啟動它。
create-react-app myapp cd myapp npm start
接下來,開啟 App.css (src/App.css) 並刪除所有 CSS 類。
// remove all css classes
接下來,建立一個簡單的 Web Components,HelloMessage (public/WebComponents/HelloMessage.js) 並新增以下程式碼。該 Web Components 的目的是歡迎使用者(透過在 Web Components 的 name 屬性中指定使用者名稱)。
// web component class HelloMessage extends HTMLElement { constructor() { super(); this.name = 'Folks'; } static get observedAttributes() { return ['name']; } attributeChangedCallback(property, oldValue, newValue) { if (oldValue === newValue) return; this[property] = newValue; } connectedCallback() { this.textContent = `Hello ${this.name}!`; } } customElements.define('hello-message', HelloMessage);
這裡,
connectedCallback() 用於建立 Web Components 的內容。
observedAttributes 函式訪問 name 屬性。
attributeChangedCallback 函式更新 name 屬性的值,如果它在應用程式中發生更改。
customElements.define 用於將建立的 Web Components 與標籤名稱附加到 Web 文件中。
接下來,開啟 index.html (public/index.html) 檔案,並新增如下所示的 Web Components:
<!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" /> <script src="%PUBLIC_URL%/WebComponents/HelloMessage.js"></script> </head> <body> <noscript>You need to enable JavaScript to run this app.</noscript> <div style="padding: 10px;"> <div id="root"></div> <div style="padding: 10px;"> <hello-message name="John"></hello-message> </div> </div> </body> </html>
這裡我們有,
在 head 部分包含了 Web Components
在頁面中使用了 hello-message Web Components 來展示其用法
接下來,建立一個簡單的元件,SimpleWebComponent (src/Components/SimpleWebComponent.js) 並渲染新建立的 Web Components,如下所示:
import React from "react"; class SimpleWebComponent extends React.Component { constructor(props) { super(props) } render() { return ( <hello-message name="Peter"></hello-message> ); } } export default SimpleWebComponent;
這裡,Web Components hello 用於元件的 render 方法中。
接下來,開啟 App 元件 (src/App.js),並使用 SimpleWebComponent 元件,如下所示:
import './App.css' import React from 'react'; import SimpleWebComponent from './Components/SimpleWebComponent' function App() { return ( <div className="container"> <div style={{ padding: "10px" }}> <div> <SimpleWebComponent /> </div> </div> </div> ); } export default App;
這裡我們有,
從 React 包中匯入了 SimpleWebComponent 元件
使用了 SimpleWebComponent 元件並渲染了 hello Web Components
最後,在瀏覽器中開啟應用程式並檢查最終結果。

總結
React 和 Web Components 以一種很好的方式相互補充。每個都有其自身的優點和缺點,並且可以透過分析其相對於應用程式的優缺點在單個應用程式中使用。