TimeAgo react component with realtime updates

July 13, 2024 (3mo ago)

A little TimeAgo react component, that updates time, in real time

import dayjs from 'dayjs';
import { useEffect, useState } from 'react';
 
export function TimeAgo({ date }) {
    const [time, setTime] = useState(dayjs());
 
    useEffect(() => {
        const interval = setInterval(() => {
            setTime(dayjs());
        }, 1000);
 
        return () => clearInterval(interval);
    });
 
    return <span>{dayjs(date).from(time)}</span>;
}

Reference : pinkary.com/@nunomaduro