Registry / serialization / react-moment-proptypes

react-moment-proptypes

JSON →
library1.8.1jsnpmunverified

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.

npm install react-moment-proptypes
INSTALL
IMPORT
SIG · REACT-MOMENT-PROPT
R
react-moment-proptypes
serializationjavascriptv1.8.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

momentPropTypes
import momentPropTypes from 'react-moment-proptypes';
const momentPropTypes = require('react-moment-proptypes');
While the README shows CommonJS `require`, the package likely exports as a default. For modern ESM environments, use the default import. CommonJS `require` is also fully supported.
momentObj
import momentPropTypes from 'react-moment-proptypes'; momentPropTypes.momentObj
This is accessed as a property of the default export. It validates if a prop is an instance of a Moment.js object.
momentString
import momentPropTypes from 'react-moment-proptypes'; momentPropTypes.momentString
Accessed as a property of the default export. It validates if a prop is a string that Moment.js can successfully parse into a date.

This quickstart demonstrates how to define and use `react-moment-proptypes` within a React component. It showcases `momentObj` for Moment objects, `momentString` for date strings, and `momentDurationObj` for Moment durations, including using `withPredicate` for custom validation.

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;
Debug
Known issues
breakingThe 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.
fix
If your application relies on custom `moment.createFromInputFallback` behavior, ensure it is defined globally or within your application logic, outside of `react-moment-proptypes`.
affects: >=1.6.0
breakingVersion 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.
fix
Upgrade your Node.js environment to version 8.x or newer to ensure compatibility and receive security updates for development dependencies.
affects: >=1.7.0
gotchaMoment.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.
fix
Refer to the Moment.js documentation on warnings (http://momentjs.com/guides/#/warnings/js-date/) and ensure that strings passed to `momentString` props conform to recognized formats or handle `createFromInputFallback` appropriately.
affects: >=1.0.0
gotchaThe `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.
fix
Always use `package-lock.json` or `yarn.lock` in your *application* projects to ensure reproducible builds, regardless of whether a library provides its own lock file.
affects: >=1.6.0
Errors
Common errors & fixes
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.
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.
fix
Ensure 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')
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.
fix
Verify 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`.
Upgrade
Version history
1.8.1latest on npm
Audit
Dependencies
momentrequiredCore functionality relies on the Moment.js library for date/time objects and parsing.
Agent activity
4 hits · last 30 days
node
4
Resources