Skip to content

Timers

A countdown or a “5m ago” label changes every second, whatever the user is doing. Putting the current time in your store would re-render every subscriber on every tick and flood the action log, so @input-actions/timers keeps it outside, in one shared clock. Your store holds the fact that matters (offer.expiresAt); the hooks derive what to show from it. Data flow explains the principle.

The package doesn’t depend on core; the hooks work in any React app.

Terminal window
npm install @input-actions/timers
HookReturnsRe-renders
useCountdown(expiresAt){ remainingMs, isActive, isExpired }Every second while active.
useTimeAgo(timestamp)'just now', '5m ago', '3h ago', '2d ago'Only when the string changes.
useExpiry(target, onExpire)nothing; calls onExpire once at targetNever.
useClock()Date.now()Every second.

Two plain functions format without a hook: formatCountdown(ms) gives 'M:SS', and timeAgo(timestamp, now) gives the same strings as useTimeAgo.

Store the deadline, show the countdown, and change state when it’s reached:

import { useDispatch, useStore } from '@input-actions/core';
import { formatCountdown, useCountdown, useExpiry } from '@input-actions/timers';
function OfferBanner() {
const expiresAt = useStore<AppState, number | null>((s) => s.offer.expiresAt);
const { remainingMs, isActive } = useCountdown(expiresAt);
if (!isActive) return null;
return <div>Offer ends in {formatCountdown(remainingMs)}</div>;
}
function OfferExpiryWatch() {
const expiresAt = useStore<AppState, number | null>((s) => s.offer.expiresAt);
const dispatch = useDispatch();
useExpiry(expiresAt, () => dispatch(['action/save', ['offer', 'expiresAt'], null]));
return null;
}

Pass null to either hook when there’s no deadline: useCountdown returns an inactive view and useExpiry does nothing. useExpiry re-arms when the target changes, fires straight away if it’s already past, and cleans up on unmount. Its callback can be an inline function; changing it doesn’t restart the timer.

function TransactionRow({ tx }: { tx: Transaction }) {
return <span>{useTimeAgo(tx.createdAt)}</span>;
}

A hundred rows share one clock, and each re-renders only when its own label changes, from '5m ago' to '6m ago'. A useState + setInterval per row would re-render every row every second.

One setInterval ticks once a second, and only while at least one component is subscribed. Every hook subscribes to it through useSyncExternalStore, with a selector for the value it derives, so React skips the re-render when that value hasn’t changed.

The clock is shared across the module, so reset it between tests. The helpers are on a separate entry point, so app code doesn’t reach for them:

import { act, render, screen } from '@testing-library/react';
import { __advanceForTest, __resetForTest } from '@input-actions/timers/testing';
beforeEach(() => __resetForTest());
test('label moves on after a minute', () => {
const createdAt = Date.now();
render(<Label at={createdAt} />);
expect(screen.getByText('just now')).toBeTruthy();
// Set the clock and notify subscribers synchronously; no fake timers needed.
act(() => __advanceForTest(createdAt + 60_000));
expect(screen.getByText('1m ago')).toBeTruthy();
});

useExpiry uses a real setTimeout rather than the shared clock, so test it with your runner’s fake timers (vi.useFakeTimers() / jest.useFakeTimers()).

The pure formatters need no setup: expect(timeAgo(t, t + 3_600_000)).toBe('1h ago').

See the API reference for every signature.