React Date and Date Range Picker
react-date-range is a React component library designed for selecting single dates or date ranges, offering a highly configurable and stateless approach to date management. As of version 2.0.1, it leverages `date-fns` for underlying date operations, requiring it as a peer dependency (minimum `date-fns` v2.0.0-alpha.1). It features multiple range selection, drag-and-drop capabilities, and keyboard navigation, using native JavaScript Date objects for all inputs and outputs. While previously actively maintained, the project's maintainers have stated it is currently unmaintained and will not be updated in the foreseeable future, making its release cadence effectively paused. Key differentiators include its flexibility, emphasis on native Date objects, and `date-fns` agnosticism (via peer dependency) over deprecated libraries like Moment.js.
Common errors
-
TypeError: date_fns__WEBPACK_IMPORTED_MODULE_2__.format is not a function
cause Using `react-date-range` with a `date-fns` version older than 2.0.0 (e.g., v1.x) without providing specific format props compatible with the older `date-fns` API, after `react-date-range` adopted `date-fns` v2+ compatibility.fixUpgrade `date-fns` to version `^2.0.0` or higher in your project's `package.json`. If an upgrade is not feasible, explicitly provide `dateDisplayFormat` and other format-related props that are compatible with your specific `date-fns` v1.x version. -
ReferenceError: moment is not defined
cause Your application code relies on `moment.js` being implicitly available or used internally by `react-date-range` after the `v1.0.0-alpha0` breaking change, which removed `moment.js` dependency.fix`react-date-range` no longer bundles or depends on `moment.js` since `v1.0.0-alpha0`. All date inputs and outputs are native `Date` objects. Refactor your code to use native `Date` objects or `date-fns` directly for date manipulations. -
RangeError: Invalid time value
cause An invalid `Date` object (e.g., `new Date(null)`, `new Date('invalid-string')`, or a `null`/`undefined` value) is passed to a `date` or `ranges` prop of `Calendar` or `DateRangePicker`, causing `date-fns` to fail during date operations.fixEnsure all `Date` objects passed to `react-date-range` components are valid instances. Always validate date inputs from external sources before passing them to the component, for example, by checking `!isNaN(newDate.getTime())`.
Warnings
- breaking `date-fns` is now a peer dependency requiring version `>=2.0.0-alpha.1`. Using older `date-fns` versions (pre-v2) will cause issues, particularly with unicode tokens in `date-fns` format strings, unless specific compatibility props are passed.
- breaking `Calendar` and `DateRange` components became fully controlled, meaning they now require explicit `date` or `ranges` props and `onChange` handlers. Additionally, input and output values are strictly native `Date` objects, and `moment.js` is no longer used or bundled out-of-the-box.
- gotcha The project is officially unmaintained by its current owners, meaning no new features, bug fixes, or security patches are expected in the foreseeable future. This significantly increases the risk for long-term production use.
- gotcha The project's license changed in 2020 and ownership was transferred to Hypeserver due to Adphorus being acquired. Review the updated license terms to ensure compliance for your use case.
- gotcha Versions prior to `1.1.4` (specifically `1.1.3`) had known issues with installing third-party dependencies and handling `date-fns` as a peer dependency.
Install
-
npm install react-date-range -
yarn add react-date-range -
pnpm add react-date-range
Imports
- Calendar
const Calendar = require('react-date-range').Calendar;import { Calendar } from 'react-date-range'; - DateRangePicker
import DateRangePicker from 'react-date-range/lib/DateRangePicker';
import { DateRangePicker } from 'react-date-range'; - DateRange
require('react-date-range').DateRange;import { DateRange } from 'react-date-range'; - Styles
import 'react-date-range/dist/styles.css'; import 'react-date-range/dist/theme/default.css';
Quickstart
import React, { useState } from 'react';
import { DateRangePicker } from 'react-date-range';
import 'react-date-range/dist/styles.css'; // main style file
import 'react-date-range/dist/theme/default.css'; // theme css file
function MyDateRangePicker() {
const [state, setState] = useState([
{
startDate: new Date(),
endDate: null,
key: 'selection'
}
]);
const handleSelect = (ranges) => {
setState([ranges.selection]);
console.log('Selected range:', ranges.selection);
// You would typically update your application state here
// e.g., send ranges.selection.startDate and ranges.selection.endDate to a parent component or API
};
return (
<div>
<h2 style={{ marginBottom: '15px' }}>Select a Date Range</h2>
<DateRangePicker
ranges={state}
onChange={handleSelect}
showSelectionPreview={true}
moveRangeOnFirstSelection={false}
months={2} // Display two months
direction="horizontal"
/>
</div>
);
}
// To use this component in a React app, you'd typically render it:
// import { createRoot } from 'react-dom/client';
// const container = document.getElementById('root');
// const root = createRoot(container);
// root.render(<MyDateRangePicker />);