React Timezone Select Component and Hook
react-timezone-select is a React component and hook designed to provide a usable and dynamic timezone selection interface. It uniquely addresses common challenges by automatically adjusting displayed choices for Daylight Savings Time (DST) and presenting a concise list of options through intelligent deduplication. The package is currently at version 3.3.3, indicating active development with frequent patch and minor updates. A key differentiator is its flexibility: developers can either use the pre-built `TimezoneSelect` component, which integrates with `react-select`, or leverage the `useTimezoneSelect` hook to integrate timezone selection logic with their own custom select components. It ships with comprehensive TypeScript types, ensuring a robust developer experience.
Common errors
-
Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/react-timezone-select/dist/index.mjs not supported.
cause Attempting to use CommonJS `require()` syntax to import `react-timezone-select` after it transitioned to ESM-only.fixSwitch to ES Module `import` syntax: `import TimezoneSelect from 'react-timezone-select';`. Ensure your environment (Node.js, bundler) is configured for ESM. -
TypeError: Cannot read properties of undefined (reading 'label') or 'value' for TimezoneSelect component.
cause The `react-select` peer dependency is not installed, or there's an issue with its version compatibility.fixVerify `react-select` is installed (`npm install react-select`) and that its version (`^5.9.0`) is compatible with `react-timezone-select`. Check for other `react-select` related warnings in the console. -
Property 'maxAbbrLength' does not exist on type 'IntrinsicAttributes & ITimezoneSelectProps'.
cause Using the deprecated `maxAbbrLength` prop after it was removed in version 3.0.2.fixRemove the `maxAbbrLength` prop from your `TimezoneSelect` component usage. This prop is no longer supported. -
TypeError: TimezoneSelect is not a function or TimezoneSelect is undefined.
cause Incorrect import syntax: trying to destructure a named import `{ TimezoneSelect }` when it is a default export.fixUse a default import: `import TimezoneSelect from 'react-timezone-select';`.
Warnings
- breaking The package transitioned to ESM-only (ES Modules) distribution. CommonJS `require()` statements will no longer work directly.
- breaking The `maxAbbrLength` prop was removed, which controlled the maximum length of timezone abbreviations displayed.
- gotcha The `TimezoneSelect` component relies on `react-select` as a peer dependency, which must be installed separately. If `react-select` is not installed, the `TimezoneSelect` component will not function.
- gotcha When using this component in applications with React Server Components (RSC), it is crucial to ensure the client directive `"use client";` is correctly applied to the component or its parent to prevent rendering errors.
- gotcha Passing `null` as the `value` prop could cause crashes in versions prior to v3.2.7 for certain scenarios.
Install
-
npm install react-timezone-select -
yarn add react-timezone-select -
pnpm add react-timezone-select
Imports
- TimezoneSelect
import { TimezoneSelect } from 'react-timezone-select'; const TimezoneSelect = require('react-timezone-select');import TimezoneSelect from 'react-timezone-select';
- ITimezone
import { ITimezone } from 'react-timezone-select';import type { ITimezone } from 'react-timezone-select'; - useTimezoneSelect
import useTimezoneSelect from 'react-timezone-select'; const { useTimezoneSelect } = require('react-timezone-select');import { useTimezoneSelect } from 'react-timezone-select'; - allTimezones
import allTimezones from 'react-timezone-select';
import { allTimezones } from 'react-timezone-select';
Quickstart
import React, { useState } from 'react';
import ReactDOM from 'react-dom/client';
import TimezoneSelect, { type ITimezone } from 'react-timezone-select';
const App = () => {
const [selectedTimezone, setSelectedTimezone] = useState<ITimezone | string>(
Intl.DateTimeFormat().resolvedOptions().timeZone,
);
return (
<div className="App">
<h2>react-timezone-select</h2>
<blockquote>Please make a selection</blockquote>
<div style={{ maxWidth: '600px', margin: '20px auto' }}>
<TimezoneSelect value={selectedTimezone} onChange={setSelectedTimezone} />
</div>
<h3>Output:</h3>
<div
style={{
backgroundColor: '#ccc',
padding: '20px',
margin: '20px auto',
borderRadius: '5px',
maxWidth: '600px',
}}
>
<pre
style={{
margin: '0 20px',
fontWeight: 500,
fontFamily: 'monospace',
}}
>
{JSON.stringify(selectedTimezone, null, 2)}
</pre>
</div>
</div>
);
};
const rootElement = document.getElementById('root');
if (rootElement) {
ReactDOM.createRoot(rootElement).render(<App />);
} else {
console.error('Root element not found');
}