React Odometer.js

3.1.3 · active · verified Sun Apr 19

React Odometer.js is a wrapper component that integrates the Odometer.js library into React applications, providing animated numerical counters. The current stable version is 3.1.3. While it doesn't follow a strict release cadence, updates appear to coincide with significant React API changes or new features, such as the adoption of hooks in v3.0.0 for React 16.8+. Its primary differentiator is simplifying the use of Odometer.js within a React ecosystem, abstracting away direct DOM manipulation typically required by the underlying library. It ships with TypeScript types, enhancing developer experience and type safety. A key aspect of its usage is managing server-side rendering (SSR) environments like Next.js or Gatsby, where dynamic imports are necessary to prevent issues with Odometer.js's reliance on the browser's `document` object.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `react-odometerjs` into a React component, updating its value after a delay to show the animation. It also highlights basic styling and essential CSS theme import requirements.

import React, { useEffect, useState } from 'react';
import Odometer from 'react-odometerjs';
// To use themes, you must install the original 'odometer' package:
// npm install odometer
// Then, import the desired theme's CSS file. Example for default theme:
import 'odometer/themes/odometer-theme-default.css';

const OdometerExample = () => {
    const [value, setValue] = useState(1234);
    const [isMounted, setIsMounted] = useState(false); // State to ensure Odometer is mounted on client-side

    useEffect(() => {
        // This ensures the Odometer component is only rendered client-side,
        // which is crucial for Odometer.js as it relies on the 'document' object.
        // For SSR frameworks, more robust dynamic imports are needed (see warnings).
        setIsMounted(true);

        const timeoutId = setTimeout(() => setValue(4321), 2000);
        return () => {
            clearTimeout(timeoutId);
        };
    }, []);

    // Render a placeholder or nothing if not mounted to prevent SSR issues
    if (!isMounted) {
        return <div>Loading counter...</div>;
    }

    return (
        <div style={{ padding: '20px', fontFamily: 'Arial, sans-serif' }}>
            <h1>Animated Counter Example</h1>
            <p>Watch the number change after 2 seconds:</p>
            <Odometer
                value={value}
                format="(.ddd),dd" // Example format: thousands separator and two decimal places (not shown here as value is integer)
                theme="default"    // Corresponds to 'odometer-theme-default.css'
                animation="count"  // Simple counting animation
                duration={1500}    // Animation duration in milliseconds
                style={{ fontSize: '4em', fontWeight: 'bold', color: '#007bff' }}
            />
            <p style={{ marginTop: '20px' }}>Current value: {value}</p>
        </div>
    );
};

export default OdometerExample;

view raw JSON →