
Build 10x products in minutes by chatting with AI - beyond just a prototype.
Topics
React Dates is a mobile-friendly datepicker library for the web, created and maintained by Airbnb. This library provides a set of React components to create date pickers, date range selectors, and calendars. It is easily internationalizable, accessible, and can be fully controlled via props.
To start using Airbnb's React Dates, you need to have React and Moment.js installed in your project. If not, you can add them using npm or yarn. Here's how you can install these packages:
After installing the necessary packages, you can import the DateRangePicker component from the react-dates library.
To use the DateRangePicker, you must maintain a state in your component for the start and end dates. The state will also keep track of whether the datepicker is focused or not.
The DateRangePicker is a fully controlled component, meaning you can control its state using props. The onDatesChange prop is a function that will be called when the dates change. The focusedInput prop determines which input field (start date or end date) is currently focused.
The phrase prop can be used to customize the text displayed in the datepicker, making it easily internationalize.
React Dates is designed to be mobile-friendly and accessible. It supports touch events and keyboard navigation for accessibility. The orientation of the datepicker can be changed to vertical for smaller screens.
You can customize the appearance of the datepicker by overriding the CSS styles. The react-dates library provides a CSS file that you can import and override.
You can also change the orientation of the datepicker to horizontal or vertical using the orientation prop.
Here is a practical example of how to implement a date range picker and display the selected dates.
In this example, the selected start and end dates are displayed below the datepicker. The format method from Moment.js is used to format the dates.
import React from 'react';
import { DateRangePicker } from 'react-dates';
import 'react-dates/initialize';
import 'react-dates/lib/css/_datepicker.css';npm install react react-dom moment react-datesimport React from 'react';
import { DateRangePicker } from 'react-dates';import React, { useState } from 'react';
import { DateRangePicker } from 'react-dates';
const MyComponent = () => {
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null);
const [focusedInput, setFocusedInput] = useState(null);
return (
<DateRangePicker
startDate={startDate}
endDate={endDate}
onDatesChange={({ startDate, endDate }) => {
setStartDate(startDate);
setEndDate(endDate);
}}
focusedInput={focusedInput}
onFocusChange={focusedInput => setFocusedInput(focusedInput)}
/>
);
};<DateRangePicker
startDatePlaceholderText="Start Date"
endDatePlaceholderText="End Date"
phrases={{ closeDatePicker: 'Close', clearDates: 'Clear dates' }}
/><DateRangePicker
orientation="vertical"
/>import 'react-dates/lib/css/_datepicker.css';<DateRangePicker
orientation="horizontal"
/>import React, { useState } from 'react';
import { DateRangePicker } from 'react-dates';
const MyComponent = () => {
const [startDate, setStartDate] = useState(null);
const [endDate, setEndDate] = useState(null);
const [focusedInput, setFocusedInput] = useState(null);
return (
<div>
<DateRangePicker
startDate={startDate}
endDate={endDate}
onDatesChange={({ startDate, endDate }) => {
setStartDate(startDate);
setEndDate(endDate);
}}
focusedInput={focusedInput}
onFocusChange={focusedInput => setFocusedInput(focusedInput)}
/>
{startDate && endDate && (
<p>
Selected dates: {startDate.format('MM/DD/YYYY')} - {endDate.format('MM/DD/YYYY')}
</p>
)}
</div>
);
};