React Moment Component
react-moment is a React component that acts as a wrapper for the widely-used `moment.js` date and time library. It enables developers to easily display formatted dates and times within React applications, leveraging Moment's extensive parsing, formatting, and manipulation capabilities. The current stable version, 1.2.2, integrates with `moment.js` versions 2.29.0 and higher. While `moment.js` itself is now considered a legacy project with its maintainers recommending modern alternatives like Luxon, date-fns, or Day.js, `react-moment` remains functional for projects still relying on the Moment.js ecosystem. It provides a declarative, component-based API for Moment's features, including relative time ("from now"), calendrical formatting, and custom date displays, often with built-in interval updates. The library also ships with TypeScript types, enhancing the developer experience in typed React projects.
Common errors
-
Module not found: Can't resolve 'moment' in 'node_modules/react-moment/dist'
cause The `moment` package, a peer dependency of `react-moment`, is not installed in your project.fixInstall `moment` by running: `npm install --save moment react-moment`. -
TypeError: Cannot read properties of undefined (reading 'tz') or moment(...).tz is not a function
cause Attempting to use `moment-timezone` features (like the `timezone` prop) without installing the `moment-timezone` package or importing it into your application.fixInstall `moment-timezone`: `npm install --save moment-timezone`, and then add `import 'moment-timezone';` to your application's main entry file or the relevant component. -
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause Incorrectly importing `Moment` as a named export instead of a default export.fixChange your import statement from `import { Moment } from 'react-moment';` to `import Moment from 'react-moment';`. -
Warning: Failed prop type: The prop `date` is marked as required in `Moment`, but its value is `undefined`.
cause The `Moment` component was rendered without providing a date value, either via the `date` prop or as a child.fixEnsure the `Moment` component receives a valid date value: `<Moment date={myDateVar} />` or `<Moment>{myDateVar}</Moment>`.
Warnings
- breaking The underlying `moment.js` library is officially deprecated and its maintainers recommend migrating to modern alternatives like Luxon, date-fns, or Day.js for new projects. `react-moment` builds directly on this legacy library.
- gotcha The `moment-timezone` package is required for timezone-specific operations (e.g., the `timezone` prop) and must be installed and imported separately.
- gotcha The `interval` prop, which controls how often the component re-renders for relative time displays (like `fromNow`), defaults to 60000 milliseconds (60 seconds).
- gotcha The `moment` library, a peer dependency, significantly increases bundle size and does not work well with modern tree-shaking algorithms, especially when including internationalization or timezone support.
- gotcha Missing `prop-types` as a peer dependency can lead to console warnings in development mode, particularly with older React versions or specific build configurations.
Install
-
npm install react-moment -
yarn add react-moment -
pnpm add react-moment
Imports
- Moment
import { Moment } from 'react-moment';import Moment from 'react-moment';
- MomentProps
import type { MomentProps } from 'react-moment'; - startPooledTimer
import { startPooledTimer } from 'react-moment';import Moment from 'react-moment'; Moment.startPooledTimer();
Quickstart
import React from 'react';
import Moment from 'react-moment';
import type { FC } from 'react';
const App: FC = () => {
const dateToFormat = '1976-04-19T12:59-0500';
// Use a dynamic date for 'fromNow' to show updates.
// Example: 1 hour from now
const futureDate = new Date(Date.now() + 60 * 60 * 1000);
return (
<div>
<h1>React Moment Examples</h1>
<p>Original date: <code>{dateToFormat}</code></p>
<p>Standard format: <Moment date={dateToFormat} /></p>
<p>Custom format: <Moment date={futureDate} format="MMMM Do YYYY, h:mm:ss a" /></p>
{/* fromNow updates automatically by default every 60s, explicitly setting interval */}
<p>Relative time (from now, updates every 30s): <Moment fromNow interval={30000}>{futureDate}</Moment></p>
<p>Calendar time (e.g., Today at 3:00 PM): <Moment calendar>{new Date()}</Moment></p>
<p>Just the current time: <Moment date={new Date()} format="HH:mm:ss" interval={1000} /></p>
<p>Date passed as child: <Moment>{dateToFormat}</Moment></p>
</div>
);
};
export default App;