Mastering react-datepicker: A comprehensive guide for developers

Blog article hero image
Allie Beazell
Allie Beazell
Retool

Nobody likes working with dates (especially in Javascript), but unfortunately it's inevitable. If your app requires a date picker component (think: users select a date from a calendar), HackerOne has you covered with the react-datepicker library. This guide will walk through react-datepicker basics with a focus on building internal tools.

By the end of this article, you’ll know how to:

  • Import and use react-datepicker in your React app to build a simple datepicker
  • Customize your datepicker with time functionality, disabling dates, and a clear button

If you just want to grab the code and go, check out the tl;dr at the end of the article.

One note before we start: this article has been updated for 2024 but was originally published in 2020.

react-datepicker: What is it and why is it useful?

react-datepicker is a React library that is used by more than 237K developers with almost 8K stars on GitHub. The simple version of the datepicker component is incredibly easy to use and comes with some useful features like:

  • Localization
  • Accessibility
  • Advanced customization
  • Range support

Because it uses date-fns instead of moment, the entire library comes out to 437kB. It runs on the latest versions of Chrome, Firefox, and IE10+.

In this tutorial, we'll build an internal tool for a customer support team. Let’s say that we’ve already built a table that displays order data using react-table—that's a great first step! But to make this tool truly useful, customer support reps need to be able to filter their view to a specific set of dates. That's where datepicker comes in.

The customer support rep needs to be able to:

  • Open a calendar view when the datepicker is clicked
  • Select a range of date values and times
  • Not select future dates
  • Clear the datepicker by clicking the “X” button

In other words, here's how the final product should look and feel:

Blog post image

How to build a datepicker for an internal tool using react-datepicker

Stand up a simple datepicker

If you're just here for the code, go ahead and jump to the TL;DR syntax summary.

Let’s keep things simple to start off: we’ll create one datepicker that only allows the user to select a single date field—no other bells and whistles.

We'll leverage the useState() hook in our function. If you’re not familiar with React Hooks, we recommend checking out React’s Hooks docs before starting this tutorial.

There are three simple steps for creating a datepicker:

  • Import the datepicker component from react-datepicker and react-datepicker.css for styling.
  • Set an initial date in the state (using the useState() Hook).
  • Render the datepicker, telling onChange to update the date in state using the setDate() function.

And, voila, you have a datepicker that starts on today’s date and will open a calendar to select a new date when clicked!

Blog post image

Create a datepicker range

The first feature we are going to add is the ability to set a date range on our datepicker. We want our customer support reps to be able to narrow down orders that happened between a specific set of dates.

react-datepicker doesn’t have native support for ranges, but we can get around it by doubling the datepicker component: one for the start date and one for the end date. Now that we already have our first datepicker, we simply need to adjust it to specifically handle a start date:

Then, we'll create a second datepicker that can handle the end date. Note that the endDate datepicker needs a minDate to be set. Since we’re picking a range, we can’t have the endDate be earlier than the startDate (time doesn’t work like that!).

And that’s it! The final version of the code all put together will look like this:

Now we have two datepickers that the customer support reps can use to select their ranges. Plus, the react-datepicker library already handles highlighting the selected dates for us.

Blog post image

Add time granularity

Let’s assume that our example company has thousands of rows of data, filled with row after row of customer order data. Even if reps only select a couple days, they’ll still get flooded with a ton of data. To make life easier for them, let’s add a time option to the datepicker so that reps can filter things down to an even more granular level.

react-datepicker comes with two options for adding time to the calendar view:

Blog post image

While the input version can be very useful since it lets the user type in any time they want, we’re going to go with the select version because we’re okay with only letting our customer support reps choose times in half-hour increments.

To add the time selector to our datepickers, we’ll first add showTimeSelect to our datepicker component to let it know we want to display the time selector, and then we’ll format the date that’s displayed in the datepicker window so that it shows time too.

Hey, that took almost no time at all (sorry).

Blog post image

Extend your datepicker with more features

We've just barely scratched the surface of what react-datepicker can do. A few useful ones:

Add a placeholder prompt to the datepicker

Instead of having the datepicker start on today’s date, why don’t we prompt the user to enter a start and end date so that our range datepicker is a little more clear? To do this, we need to add a placeholderText field and change the initial startDate and endDate values to null.

Blog post image

Now, the user can easily choose a start and end date and time in a logical order.

Disable future dates

Since our reps are dealing with orders from customers, all of the data in the table they are viewing will be in the past. If they were to select a date in the future, there would be no data to view, and the rep would be looking at an empty table. Instead of that happening, lets disable all future dates in the calendar so that the rep can’t select them.

For this feature, we’re going to add the filterDate field and define an arrow function that returns a Boolean value depending on whether the date displayed is in the future or not.

Great! Now the rep will only be able to filter the table based on dates that actually have order data, and we avoid time travel paradoxes.

Blog post image

Add a button to clear the datepicker

If the customer support rep decides that they no longer want to filter by date, we want to make it easy for them to clear the filter. Let’s give them a simple X they can press to clear the datepicker.

This one is just a simple Boolean prop type that is passed in to datepicker. Here is what the default clear button looks like:

Blog post image

At this point we should probably let you know that we added a little extra styling to make the datepicker look this way. If you choose to display the time in your datepicker, the clear button sits right on top of it, like this:

Blog post image

In order to expand the width of the datepicker boxes, we have to override some of the styling that we imported in react-datepicker.css. To do this, we are going to use Styled Components, a popular React library used for low-level styling.

First, import styled-components, and then define a new Styles component that will wrap around your datepicker. Then, move all of your datepicker code into a new function. You want your default function to export just your datepicker code (all wrapped up) with the <Styles> component around it.

Take careful note of the react-datepicker classnames that must be overwritten:

  • react-datepicker-wrapper
  • react-datepicker__input-container
  • react-datepicker__input-container input

And now that we’re overwriting classnames from react-datepicker, we might as well change up the style of our clear button. To override the button styles, you just need to change .react-datepicker__close-icon::before and .react-datepicker__close-icon::after.

Here is our new, styled X button:

Blog post image

Extract data from the datepicker

Extracting the selected date(s) from your datepicker will depend on how your code is set up. For example, if I’m within the same component, getting the startDate and endDate is as simple as accessing the state.

Here you can see we are printing the selected date below the datepickers. Note that startDate and endDate are saved as Date objects so you must convert them to Strings with the toString() method before printing (or else your IDE will yell at you).

Blog post image

If you are working with multiple React components, then you will likely need to lift the state out of the datepicker components. That work goes a bit beyond the scope of this tutorial, but you can read up on how to do it in the React docs.

Pulling it all together: Your finished datepicker component with react-datepicker

You’ve now built an intuitive datepicker for your internal app! Here is our final datepicker:

Blog post image

Hopefully, this tutorial helped you understand how to create and customize a datepicker in React to suit your needs. We know we covered a lot of features here, so for good measure, here is the code for the datepicker we created, in its entirety:

For more code samples that cover every feature react-datepicker has to offer, check out React Datepicker’s website.

Retool also offers a complete set of powerful building blocks for building internal tools. Assemble your app in 30 seconds by dragging and dropping from more than 50 pre-built components. Start building today.

TL;DR: Syntax roundup

Create a simple datepicker

Create a datepicker range

Selecting time with datepicker

Add a placeholder

Disable future dates

Add a clear button to the datepicker

Reader

Allie Beazell
Allie Beazell
Retool
Related Articles
Copied