React Date Picker Component
react-date-picker is a flexible date picker component for React applications, providing robust functionalities for selecting dates, months, and years. Its current stable version is 12.0.2. The package maintains an active release cadence, with minor and patch updates occurring frequently to address bugs and improve developer experience, while major versions are released less often, coinciding with significant architectural shifts or React version compatibility updates. Key differentiators include its lightweight nature, comprehensive TypeScript support, enhanced prop documentation via JSDoc for improved IDE assistance, and its transition to being an ESM-only package since v12.0.0, aligning with modern JavaScript module standards. It supports React versions 16.8.0 through 19.0.0 and mandates the New JSX Transform since v11.0.0.
Common errors
-
TypeError: get format called on incompatible undefined
cause An internal date formatting bug affecting specific browser environments.fixUpdate `react-date-picker` to version `12.0.2` or newer to resolve this bug fix. -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax in an environment where `react-date-picker` is an ESM-only package (v12.0.0+).fixChange your import statements from `const DatePicker = require('react-date-picker');` to `import DatePicker from 'react-date-picker';`. Verify your bundler/environment supports ESM. -
Failed prop type: DatePicker: prop type portalContainer is invalid
cause Issue with `portalContainer` prop validation, potentially during server-side rendering or with incorrect value types.fixEnsure the `portalContainer` prop, if used, is provided with a valid DOM element or reference. For SSR issues, update to `react-date-picker@10.5.1` or newer. -
ReferenceError: HTMLElement is not defined (during server-side rendering)
cause The package tried to access the browser-specific global `HTMLElement` object during SSR, which is not available in Node.js environments.fixUpdate `react-date-picker` to version `10.5.0` or newer, which includes fixes for this specific SSR compatibility issue.
Warnings
- breaking The package dropped CommonJS build, becoming ESM-only. Direct 'require()' statements will no longer work.
- breaking React's New JSX Transform is now a mandatory requirement for this package.
- breaking `propTypes` have been entirely removed from the package.
- gotcha Due to the transition to ESM-only, running unit tests with Jest might encounter compatibility issues.
Install
-
npm install react-date-picker -
yarn add react-date-picker -
pnpm add react-date-picker
Imports
- DatePicker
import { DatePicker } from 'react-date-picker'; const DatePicker = require('react-date-picker');import DatePicker from 'react-date-picker';
- Value
import type { Value } from 'react-date-picker'; - DatePicker.css
import 'react-date-picker/dist/DatePicker.css'; import 'react-calendar/dist/Calendar.css';
Quickstart
import React, { useState } from 'react';
import DatePicker from 'react-date-picker';
import 'react-date-picker/dist/DatePicker.css';
import 'react-calendar/dist/Calendar.css';
type ValuePiece = Date | null;
type Value = ValuePiece | [ValuePiece, ValuePiece];
function MyDatePickerComponent() {
const [value, onChange] = useState<Value>(new Date());
return (
<div>
<h1>Date Selection Example</h1>
<p>Select a date using the React Date Picker:</p>
<DatePicker onChange={onChange} value={value} />
{value && Array.isArray(value) ? (
<p>Selected date range: {value[0]?.toDateString() || 'None'} - {value[1]?.toDateString() || 'None'}</p>
) : (
<p>Selected date: {value?.toDateString() || 'None'}</p>
)}
<p>Current value in state: {JSON.stringify(value)}</p>
</div>
);
}
export default MyDatePickerComponent;