React Time Picker
`react-time-picker` is a React component that provides an intuitive interface for users to select a specific time. It is actively maintained, with the current stable version being 8.0.3, released recently with improvements like OIDC trusted publishing and immutable releases. The library generally follows a stable release cadence, with major versions addressing significant breaking changes or new React features, and minor/patch versions focusing on bug fixes and enhancements. Key differentiators include its focus on accessibility, ease of integration into existing React projects, and reliance on native browser datetime capabilities where appropriate, while offering extensive customization for internationalization and display formats. It provides a clean, unopinionated UI that integrates well into various design systems, offering flexibility without enforcing a specific look and feel, and ships with TypeScript types for improved developer experience.
Common errors
-
TypeError: get format called on incompatible undefined
cause An internal issue with date formatting logic, particularly in certain browser environments.fixUpdate `react-time-picker` to version 8.0.3 or newer, as this bug was fixed in that release. -
SyntaxError: Cannot use import statement outside a module
cause Occurs when running Jest tests because `react-time-picker` is an ESM-only package (since v8.0.0), and Jest typically runs in a CommonJS environment by default.fixConfigure Jest to process ES modules (e.g., via `babel-jest` with appropriate presets and plugins, or `jest-esm-transformer`), or migrate your test suite to a test runner like Vitest which natively supports ESM. -
ReferenceError: HTMLElement is not defined
cause This error occurs during Server-Side Rendering (SSR) when the component attempts to access browser-specific global objects that are not available in a Node.js environment.fixUpdate `react-time-picker` to version 6.5.0 or newer, where this SSR compatibility issue was resolved. -
Failed prop type: TimePicker: prop type portalContainer is invalid
cause An error in prop type validation specifically affecting Server-Side Rendering (SSR) environments when the `portalContainer` prop was used.fixUpdate `react-time-picker` to version 6.5.1 or newer, which contains a fix for this SSR-related prop type validation issue.
Warnings
- breaking The package dropped CommonJS build, becoming ESM-only. Direct `require()` statements will fail, and bundlers/test runners may require configuration adjustments.
- breaking The New JSX Transform is now a hard requirement. Older Babel configurations or React versions not using this transform will encounter build errors.
- breaking `propTypes` were removed from the component. Applications relying on `propTypes` for type checking will no longer receive warnings.
- gotcha Running unit tests with Jest might fail due to the package being ESM-only since v8.0.0, as Jest's default environment is CommonJS.
Install
-
npm install react-time-picker -
yarn add react-time-picker -
pnpm add react-time-picker
Imports
- TimePicker
const TimePicker = require('react-time-picker');import TimePicker from 'react-time-picker';
- TimePicker CSS
import 'react-time-picker/dist/TimePicker.css';
- Clock CSS (often needed)
import 'react-clock/dist/Clock.css';
Quickstart
import React, { useState } from 'react';
import TimePicker from 'react-time-picker';
import 'react-time-picker/dist/TimePicker.css';
import 'react-clock/dist/Clock.css'; // Often needed for the clock overlay
type ValuePiece = Date | null;
type Value = ValuePiece | [ValuePiece, ValuePiece];
function MyTimePicker() {
const [value, onChange] = useState<Value>(new Date());
return (
<div>
<h1>Select a Time</h1>
<TimePicker
onChange={onChange}
value={value}
format="HH:mm" // Example format
clearIcon={null} // Remove clear button if desired
disableClock={true} // Disable the clock popover if only input is wanted
/>
{value && <p>Selected time: {Array.isArray(value) ? value[0]?.toLocaleTimeString() : value.toLocaleTimeString()}</p>}
</div>
);
}
export default MyTimePicker;