kipluo
React6 min read3 steps

How to Animate a Number Counting Up in React

Count-up numbers are common on dashboards and landing pages. A good one animates from the current value (not from zero every time), formats currency and thousands separators correctly, doesn't shift the layout as digits change, and gives screen readers only the final value.

You needReact 18+requestAnimationFrameIntl.NumberFormat
Loading demo
Change the value and the number counts from its current value to the new one.Component page

1. Tween between the old and new value

Each frame, compute progress t from 0 to 1 based on elapsed time, pass it through an easing function, and interpolate: from + (to - from) * eased. easeOutCubic starts fast and slows into the final value, which suits numbers well.

Store the value currently on screen in a ref. When the target changes mid-animation, start the next tween from that displayed value, so the number never jumps backward.

useCountUp.ts
import { useEffect, useRef, useState } from "react";

const easeOutCubic = (t: number) => 1 - Math.pow(1 - t, 3);

export function useCountUp(target: number, duration = 900) {
  const [display, setDisplay] = useState(target);
  const current = useRef(target);

  useEffect(() => {
    const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
    if (reduce) {
      current.current = target;
      setDisplay(target);
      return;
    }
    const from = current.current;
    const start = performance.now();
    let raf = 0;

    const tick = (now: number) => {
      const t = Math.min(1, (now - start) / duration);
      const value = from + (target - from) * easeOutCubic(t);
      current.current = value;
      setDisplay(value);
      if (t < 1) raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [target, duration]);

  return display;
}

2. Format it and stop the width jitter

Format the animated value with Intl.NumberFormat so currency symbols, separators and decimals follow the user's locale. Create the formatter once with useMemo; constructing it every frame is wasteful.

In most fonts digits have different widths, so a changing number makes surrounding text wobble. font-variant-numeric: tabular-nums (Tailwind: tabular-nums) gives every digit the same width.

Stat.tsx
import { useMemo } from "react";
import { useCountUp } from "./useCountUp";

export function Stat({ value, label }: { value: number; label: string }) {
  const display = useCountUp(value);
  const fmt = useMemo(
    () => new Intl.NumberFormat(undefined, { style: "currency", currency: "USD", maximumFractionDigits: 0 }),
    [],
  );

  return (
    <div>
      <p className="text-sm text-zinc-500">{label}</p>
      {/* Screen readers get the final value once, not 50 intermediate numbers */}
      <p className="text-3xl font-bold tabular-nums" aria-hidden>
        {fmt.format(display)}
      </p>
      <span className="sr-only">{fmt.format(value)}</span>
    </div>
  );
}

3. Kipluo Count Up Stat and Rolling Counter

Kipluo has two free number components. Count Up Stat animates a labelled figure with an optional up or down percentage badge; it takes Intl.NumberFormatOptions through a format prop. Rolling Counter rolls each digit on its own spring like an odometer.

Both use tabular digits, expose the final value to assistive technology, and jump straight to the value under reduced motion.

Revenue.tsx
// npx kipluo add count-up-stat
import { CountUpStat } from "@/components/kipluo/count-up-stat";

export function Revenue({ total }: { total: number }) {
  return <CountUpStat value={total} label="Monthly revenue" delta={12.5} />;
}

Accessibility checklist

Tick each one off as you check your build. Your ticks stay in this browser.

0 of 4 checked

Build it yourself or install it

Build it yourself when

A few stats on a landing page. The hook above is about 30 lines.

Install Kipluo when

You want spring-driven digits, a delta badge, theme styles and tick sounds, and the same widgets in Flutter.

Keep building