React Calendar Component
React Calendar is a highly customizable and internationalization-friendly date picker component for React applications. Currently stable at version 6.0.1, it receives frequent updates that include new features, bug fixes, and performance improvements. It leverages modern React features like the new JSX transform and is distributed exclusively as an ES module since version 6.0.0. Key differentiators include its robust TypeScript support, extensibility through props like `tileClassName` and `tileContent`, and recent adoption of npm trusted publishing and immutable releases for enhanced supply chain security, aiming to improve overall supply chain security and reliability for users.
Common errors
-
TypeError: get format called on incompatible undefined
cause An internal date formatting issue occurred in certain browser environments.fixUpgrade `react-calendar` to version 6.0.1 or newer, which includes a fix for this specific TypeError. -
require() of ES Module ... not supported. Instead change the require of index.js to a dynamic import()
cause Attempting to import `react-calendar` using CommonJS `require()` syntax after it transitioned to an ESM-only package.fixChange all instances of `const Calendar = require('react-calendar');` to `import Calendar from 'react-calendar';` and ensure your project's build environment supports ESM. -
'React' must be in scope when using JSX
cause The project's build configuration is not set up to use the new JSX transform, which no longer requires explicit `import React from 'react'` in every file.fixEnable the automatic JSX runtime in your Babel configuration (e.g., `"plugins": [["@babel/plugin-transform-react-jsx", {"runtime": "automatic"}]]`) or TypeScript `tsconfig.json` (`"jsx": "react-jsx"`). -
Property 'propTypes' does not exist on type 'typeof Calendar'
cause Attempting to access or define `propTypes` on the `Calendar` component after `react-calendar` removed them.fixRemove any `propTypes` definitions or checks related to `react-calendar`. For type safety, use TypeScript or a separate runtime validation library.
Warnings
- breaking The package is now ESM-only. CommonJS (`require`) builds have been dropped completely.
- breaking The new JSX transform for React is now a strict requirement.
- breaking `propTypes` have been entirely removed from the component.
- gotcha Old values for the `calendarType` prop will be deprecated in future releases.
- gotcha When using Jest for unit testing, you may encounter issues due to the package becoming ESM-only since v6.
Install
-
npm install react-calendar -
yarn add react-calendar -
pnpm add react-calendar
Imports
- Calendar
const Calendar = require('react-calendar');import Calendar from 'react-calendar';
- CalendarType
import CalendarType from 'react-calendar';
import { CalendarType } from 'react-calendar'; - TileArgs
import TileArgs from 'react-calendar';
import { TileArgs } from 'react-calendar';
Quickstart
import React, { useState } from 'react';
import Calendar from 'react-calendar';
import 'react-calendar/dist/Calendar.css';
type ValuePiece = Date | null;
type Value = ValuePiece | [ValuePiece, ValuePiece];
function MyCalendar() {
const [value, onChange] = useState<Value>(new Date());
return (
<div className="my-calendar-container">
<h1>Select a Date</h1>
<Calendar onChange={onChange} value={value} />
{value instanceof Date && value && (
<p>Selected date: {value.toLocaleDateString()}</p>
)}
{Array.isArray(value) && value[0] instanceof Date && value[1] instanceof Date && (
<p>Selected range: {value[0].toLocaleDateString()} - {value[1].toLocaleDateString()}</p>
)}
</div>
);
}
export default MyCalendar;