Dev Helpers
← Back

Page-local reading timer

Place this file at app/studio/template.tsx in an App Router project.

Open this example in the interactive library

Source code

"use client";

// app/studio/template.tsx

import { useState, useEffect } from "react";

import type { ReactNode } from "react";

export default function PageLocalReadingTimer({ children }: { children: ReactNode }) {
  const [seconds, set] = useState(0);
  useEffect(() => {
    const start = Date.now();
    const t = setInterval(() => set(Math.floor((Date.now() - start) / 1000)), 1000);
    return () => clearInterval(t);
  }, []);
  return (
    <article>
      <small>{seconds}s on this page</small>
      {children}
    </article>
  );
}