Moment.js Prop Types for React
react-moment-proptypes provides custom React PropTypes for validating `moment` objects, `moment` duration objects, and strings that `moment` can parse into valid dates. It ensures that React components receive expected date/time formats, leveraging the `moment.js` library. The current stable version is 1.8.1. Releases appear to be driven by dependency updates and bug fixes rather than a strict schedule, with recent activity focusing on modernizing development dependencies and addressing compatibility issues. It is a utility layer bridging React's prop-type validation system with Moment.js's data structures, allowing developers to define robust type checks for time-related props without manual validation logic.
Common errors
-
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions.
cause A string passed to a `momentString` prop (or a raw Moment.js constructor call) is in an ambiguous or non-standard format, causing Moment.js to fall back to `new Date()`, which is unreliable.fixEnsure that string date inputs are in a clearly defined, standard format (e.g., ISO 8601 'YYYY-MM-DDTHH:mm:ssZ'). For custom formats, consider defining a `moment.createFromInputFallback` function or explicitly specifying the format when creating moment objects. -
TypeError: Cannot read properties of undefined (reading 'momentObj')
cause The `momentPropTypes` object was not correctly imported or is undefined, often due to incorrect CommonJS `require` or ESM `import` syntax, or the package itself failed to load.fixVerify your import statement: `import momentPropTypes from 'react-moment-proptypes';` for ESM or `const momentPropTypes = require('react-moment-proptypes');` for CommonJS. Ensure `react-moment-proptypes` is correctly installed in `node_modules`.
Warnings
- breaking The internal implementation of `moment.createFromInputFallback` was removed in v1.6.0. This prevents conflicts with other packages but means developers must manage `createFromInputFallback` themselves if custom parsing rules are needed.
- breaking Version 1.7.0 introduced a minimum Node.js version requirement of 8.x or above. This primarily affects development environments but could impact older build pipelines.
- gotcha Moment.js itself issues deprecation warnings when constructing dates from unrecognized RFC2822 or ISO formats. `react-moment-proptypes` uses Moment.js, so these warnings may appear in your console if `momentString` props contain ambiguous date formats.
- gotcha The `package-lock.json` file was intentionally removed from the package in v1.6.0. While this is a common practice for libraries to avoid locking consumer dependencies, it might lead to less reproducible `npm install` behavior in your own projects if you rely on the library's exact dependency tree.
Install
-
npm install react-moment-proptypes -
yarn add react-moment-proptypes -
pnpm add react-moment-proptypes
Imports
- momentPropTypes
const momentPropTypes = require('react-moment-proptypes');import momentPropTypes from 'react-moment-proptypes';
- momentObj
import momentPropTypes from 'react-moment-proptypes'; momentPropTypes.momentObj
- momentString
import momentPropTypes from 'react-moment-proptypes'; momentPropTypes.momentString
Quickstart
import React from 'react';
import PropTypes from 'prop-types';
import moment from 'moment';
import momentPropTypes from 'react-moment-proptypes';
class TestComponent extends React.Component {
render() {
return (
<div>
<p>Date Prop: {this.props.dateThing?.format('YYYY-MM-DD')}</p>
<p>UTC Date Prop: {this.props.dateThingWithPredicate?.format('YYYY-MM-DD HH:mm [UTC]')}</p>
<p>String Date Prop: {this.props.stringThing}</p>
<p>Duration Prop: {this.props.durationThing?.humanize()}</p>
</div>
);
}
}
TestComponent.propTypes = {
dateThing: momentPropTypes.momentObj,
dateThingWithPredicate: momentPropTypes.momentObj.withPredicate(
function isUTC(momentObject) {
return momentObject.isUTC();
}
),
stringThing: momentPropTypes.momentString,
durationThing: momentPropTypes.momentDurationObj
};
// Example Usage
const App = () => (
<TestComponent
dateThing={moment()}
dateThingWithPredicate={moment.utc()}
stringThing={'12-12-2014'}
durationThing={moment.duration(3, 'hours')}
/>
);
export default App;