Created
October 28, 2018 08:08
-
-
Save jackyef/aaa647ea7b4b9a8a95b64998e627b773 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useState, useEffect } from 'react'; | |
import ReactDOM from 'react-dom'; | |
const usePerformance = initialTimestamp => { | |
const [timings, updateTimings] = useState([]); | |
let prev = initialTimestamp; | |
useEffect(() => { | |
const newTimestamp = new Date().getTime(); | |
updateTimings([...timings, newTimestamp - prev]); | |
prev = newTimestamp; | |
}); | |
return timings; | |
}; | |
const App = () => { | |
const timings = usePerformance(new Date().getTime()); | |
return ( | |
<div> | |
<div>Performance timings </div> | |
<div> | |
{timings.map((time, i) => ( | |
<div key={i}> | |
Re-render number {i} took {time} | |
ms | |
</div> | |
))} | |
</div> | |
</div> | |
); | |
}; | |
ReactDOM.render(<App />, document.getElementById('root')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment