Buradasın
React'ta reportWebVitals nasıl kullanılır?
Yazeka
Arama sonuçlarına göre oluşturuldu
React'te
reportWebVitalskullanmak için aşağıdaki adımlar izlenebilir:
- Web-vitals kütüphanesini kurmak 3.
npm install web-vitals
veyayarn add web-vitals
komutlarıyla kütüphane yüklenir 3. reportWebVitals.js
dosyası oluşturmak 3. React projesindereportWebVitals.js
adında bir dosya oluşturulur 3.- Metrikleri ölçmek 3.
reportWebVitals.js
dosyasına aşağıdaki kod eklenir 3:
import { getCLS, getFID, getLCP, getFCP, getTTFB } from 'web-vitals'; function reportWebVitals(onPerfEntry) { if (onPerfEntry && typeof onPerfEntry === 'function') { getCLS(onPerfEntry); getFID(onPerfEntry); getLCP(onPerfEntry); getFCP(onPerfEntry); getTTFB(onPerfEntry); } } export default reportWebVitals;
- Web Vitals'ı React'e entegre etmek 3. Web Vitals, ana React dosyasına (
index.js
veyamain.js
) entegre edilir 3. Dosya şu şekilde değiştirilir 3:
import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import reportWebVitals from './reportWebVitals'; ReactDOM.render( <React.StrictMode> <App /> </React.StrictMode>, document.getElementById('root') ); // Web Vitals'ı ölçmeye başlamak reportWebVitals(console.log);
Bu ayarlamayla Web Vitals verileri tarayıcı konsolunda görüntülenir 3.
Analitik için veri göndermek (isteğe bağlı) 3. Web Vitals verilerini bir Google Analytics gibi bir kontrol panelinde izlemek için
reportWebVitalsişlevi şu şekilde güncellenir 3:
function sendToAnalytics(metric) { // Örnek: Google Analytics'e veri gönderme const body = JSON.stringify(metric);
5 kaynaktan alınan bilgiyle göre: