- 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 - Map
- 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 - Context
- ReactJS - 錯誤邊界
- ReactJS - 轉發Refs
- ReactJS - Fragments
- ReactJS - 高階元件
- ReactJS - 與其他庫整合
- ReactJS - 效能最佳化
- ReactJS -效能分析器API
- ReactJS - Portals
- ReactJS - 無ES6 ECMAScript的React
- ReactJS - 無JSX的React
- ReactJS - 調和
- ReactJS - Refs和DOM
- ReactJS - Render Props
- ReactJS - 靜態型別檢查
- ReactJS - Strict Mode
- ReactJS - Web Components
- 附加概念
- ReactJS - 日期選擇器
- ReactJS - Helmet
- ReactJS - 內聯樣式
- ReactJS - PropTypes
- ReactJS - BrowserRouter
- ReactJS - DOM
- ReactJS - 輪播圖
- ReactJS - 圖示
- ReactJS - 表單元件
- ReactJS - 參考API
- ReactJS 有用資源
- ReactJS - 快速指南
- ReactJS - 有用資源
- ReactJS - 討論
ReactJS -效能分析器API
效能分析是一種重要的技術,用於收集和顯示特定函式在即時環境中執行所需的時間。效能分析通常用於查詢應用程式中的效能瓶頸。React 提供了兩種效能分析React應用程式的方法。
Profiler元件
Profiler DevTools
Profiler元件
React Profiler元件只是另一個React元件,用於記錄React元件的效能資訊。Profiler元件可以用於React元素樹中的任何位置。React接受多個Profiler以及Profiler的多級巢狀。本章我們將檢查簽名以及如何在React應用程式中應用Profiler。
Profiler元件的簽名
Profiler元件可以巢狀任何React元件,並需要兩個props:
id
Profiler元件的識別符號
onRender回撥函式
在元件渲染的每個階段呼叫的回撥函式
回撥函式的簽名如下:
function onRenderCallback(
id,
phase,
actualDuration,
baseDuration,
startTime,
commitTime,
interactions
){
// Do anything with the profiler information
}
一個示例回撥函式實現,用於將效能分析資料記錄到控制檯,如下所示:
const logProfilerData = (id, phase, actualTime, baseTime, startTime, commitTime) => {
console.log(`${id}'s ${phase} phase:`);
console.log(`Actual time: ${actualTime}`);
console.log(`Base time: ${baseTime}`);
console.log(`Start time: ${startTime}`);
console.log(`Commit time: ${commitTime}`);
};
應用Profiler
在本節中,我們將建立一個新的React應用程式來學習如何在其中應用Profiler元件。
首先,使用以下命令建立一個新的React應用程式並啟動它。
create-react-app myapp cd myapp npm start
接下來,開啟App.css (src/App.css)並刪除所有CSS類。
// remove all css classes
接下來,建立一個簡單的Hello元件,Hello (src/Components/Hello.js),並渲染一條簡單的訊息,如下所示:
import React from "react";
class Hello extends React.Component {
constructor(props) {
super(props)
}
render() {
return (
<div>Hello, {this.props.name}</div>
);
}
}
export default Hello;
這裡:
使用name props來使用給定的名稱渲染Hello訊息
接下來,開啟App元件 (src/App.js),並使用Profiler元件,如下所示:
import './App.css'
import React, { Profiler } from 'react';
import Hello from './Components/Hello'
const logProfilerData = (id, phase, actualTime, baseTime, startTime, commitTime) => {
console.log(`${id}'s ${phase} phase:`);
console.log(`Actual time: ${actualTime}`);
console.log(`Base time: ${baseTime}`);
console.log(`Start time: ${startTime}`);
console.log(`Commit time: ${commitTime}`);
};
function App() {
return (
<div className="container">
<div style={{ padding: "10px" }}>
<div>
<Profiler id="helloWorld" onRender={logProfilerData}>
<Hello name="World" />
</Profiler>
</div>
</div>
</div>
);
}
export default App;
這裡:
從react包中匯入了Profiler元件
使用Hello元件,並用Profiler元件將其包裝起來
建立了一個回撥函式,logProfilerData,並將所有Profiler資料輸出到控制檯
在Profiler元件的onRender props中設定了logProfilerData回撥函式
最後,在瀏覽器中開啟應用程式並檢查最終結果。它將渲染Hello元件,如下所示:
開啟控制檯,您可以看到效能分析資訊,如下所示:
helloWorld's mount phase: App.js:7 Actual time: 4.900000000372529 App.js:8 Base time: 1.800000000745058 App.js:9 Start time: 515.7999999988824 App.js:10 Commit time: 525.9000000003725 ... App.js:6 helloWorld's update phase: App.js:7 Actual time: 1 App.js:8 Base time: 0.6999999992549419 App.js:9 Start time: 19836.900000000373 App.js:10 Commit time: 19838.400000000373
Profiler React DevTools
React DevTools外掛為Profiler引入了一個單獨的部分。開發者可以開啟Profiler選項卡,並獲得許多關於應用程式的有用見解。Profiler DevTools中提供的一些功能如下:
瀏覽提交
過濾提交
火焰圖
排名圖
元件圖
結論
React Profiler元件和Profiler DevTools是不可或缺且強大的工具,用於分析和最佳化React應用程式。