React Native Modal Datetime Picker
react-native-modal-datetime-picker is a declarative, cross-platform library that provides a unified user and developer experience for showing native date and time pickers within a modal in React Native applications. It is currently at stable version 18.0.0 and actively maintained, with frequent releases addressing bug fixes, new features, and breaking changes. This library acts as a wrapper around the `@react-native-community/datetimepicker` package, leveraging the underlying native module for iOS and Android while abstracting it into a convenient modal component. Its key differentiator is providing a modal overlay for the native pickers, which can be useful for applications requiring a clear, focused date/time selection interface, integrating seamlessly with both non-Expo and Expo projects.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'defaultProps')
cause Attempting to access `defaultProps` on `DateTimePickerModal` which has been removed.fixRemove `defaultProps` usage and pass all props explicitly or handle defaults internally. -
Argument of type 'Date | undefined' is not assignable to parameter of type 'Date'.
cause The `onCancel` callback no longer provides a `date` object, leading to type mismatches if your handler expects one.fixAdjust the `onCancel` handler signature to not expect a `date` parameter, or handle `undefined` explicitly. -
Error: `RNDateTimePicker` module not found. Are you sure you've linked all the native dependencies and rebuilt your app?
cause The underlying native module `@react-native-community/datetimepicker` was not correctly linked or the app was not rebuilt after installation.fixFor non-Expo projects, run `npx react-native link @react-native-community/datetimepicker` (if applicable for your RN version) or follow manual linking instructions, then rebuild your native project (`npx react-native run-ios` / `run-android`). For Expo, ensure `npx expo install` was used and clear cache if needed.
Warnings
- breaking The `defaultProps` functionality has been completely removed. Any reliance on `defaultProps` for `DateTimePickerModal` components will now result in errors.
- breaking The `onCancel` callback no longer receives a `date` parameter. If your `onCancel` handler was expecting a date object, it will now receive `undefined` or no arguments.
- breaking Changes to type definitions for `customCancelButton` and `customConfirmButton` props may break existing TypeScript type-checking flows.
- gotcha The underlying `@react-native-community/datetimepicker` is a native module that requires manual linking in non-Expo React Native projects. Failure to link can lead to runtime errors or the picker not appearing correctly.
- gotcha On iOS, ensuring the picker theme (light/dark) respects the device theme or displays correctly requires specific `app.json` configuration for Expo projects or manual AppDelegate setup for bare projects.
Install
-
npm install react-native-modal-datetime-picker -
yarn add react-native-modal-datetime-picker -
pnpm add react-native-modal-datetime-picker
Imports
- DateTimePickerModal
import { DateTimePickerModal } from 'react-native-modal-datetime-picker'; const DateTimePickerModal = require('react-native-modal-datetime-picker');import DateTimePickerModal from 'react-native-modal-datetime-picker';
- Props (e.g., DateTimePickerModalProps)
import type { DateTimePickerModalProps } from 'react-native-modal-datetime-picker'; - useDatePicker
import { useDatePicker } from 'react-native-modal-datetime-picker';
Quickstart
import React, { useState } from "react";
import { Button, View, Text, Platform } from "react-native";
import DateTimePickerModal from "react-native-modal-datetime-picker";
const DatePickerExample = () => {
const [isDatePickerVisible, setDatePickerVisibility] = useState(false);
const [selectedDate, setSelectedDate] = useState(new Date());
const showDatePicker = () => {
setDatePickerVisibility(true);
};
const hideDatePicker = () => {
setDatePickerVisibility(false);
};
const handleConfirm = (date) => {
console.log("A date has been picked: ", date.toISOString());
setSelectedDate(date);
hideDatePicker();
};
return (
<View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
<Text style={{ marginBottom: 20 }}>Selected Date: {selectedDate.toDateString()}</Text>
<Button title="Show Date Picker" onPress={showDatePicker} />
<DateTimePickerModal
isVisible={isDatePickerVisible}
mode="date"
date={selectedDate} // Set initial date
onConfirm={handleConfirm}
onCancel={hideDatePicker}
headerTextIOS="Pick a Date"
cancelTextIOS="Cancel"
confirmTextIOS="Confirm"
pickerContainerStyleIOS={{ backgroundColor: 'white' }} // Example iOS styling
textColor="#000" // Example Android styling
display={Platform.OS === 'ios' ? 'spinner' : 'default'} // Control picker display style
/>
</View>
);
};
export default DatePickerExample;