Skip to content

Instantly share code, notes, and snippets.

@twhite96
Forked from swyxio/Timer fallback.js
Created November 15, 2018 07:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twhite96/4b3ed040d024612255a1bf625c2118c7 to your computer and use it in GitHub Desktop.
Save twhite96/4b3ed040d024612255a1bf625c2118c7 to your computer and use it in GitHub Desktop.
Timer component for use as a Suspense fallback
function Timer() {
const startTime = React.useRef(performance.now());
const [time, setTime] = React.useState(performance.now());
React.useEffect(() => {
const id = setTimeout(() => {
ReactDOM.flushSync(() => {
setTime(performance.now());
});
}, 2);
return () => clearTimeout(id);
});
return (
<div className="Fallback">
<h3>🌀 App Level Suspense</h3>
<b>{Math.round(time - startTime.current)} ms</b>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment