A date picker is a crucial component in many web applications, enabling users to select dates conveniently. React, a popular JavaScript library, offers several libraries and packages to implement date pickers.
One of the most commonly used date picker libraries for React is react-datepicker. In this article, we will walk you through the process of adding a date picker to your React application using react-datepicker.
Before we begin, make sure you have a basic understanding of React and have a React project set up. If you haven't already, you can create a new React application using the Create React App.
To get started, you need to install the react-datepicker
package. You can do this using npm or yarn, depending on your package manager preference. Open your terminal and run one of the following commands:
Using npm:
npm install react-datepicker --save
Using yarn:
yarn add react-datepicker
Once you've installed react-datepicker
, you can import it into your React component.
import React, { useState } from 'react';
import DatePicker from 'react-datepicker';
import 'react-datepicker/dist/react-datepicker.css'; // Import CSS
Now that you have imported react-datepicker
, you can use it in your component. Here's an example of how to create a simple date picker.
function App() {
const [selectedDate, setSelectedDate] = useState(null);
return (
<div className="App">
<h1>Date Picker Example</h1>
<DatePicker
selected={selectedDate}
onChange={(date) => setSelectedDate(date)}
dateFormat="MM/dd/yyyy"
/>
</div>
);
}
export default App;
In this example, we:
DatePicker
from react-datepicker
.selectedDate
to hold the selected date.<DatePicker />
component and set its selected
prop to the selectedDate
state.onChange
event handler to update the selectedDate
state when the user selects a date.dateFormat
prop to specify the format in which the date should be displayed.
react-datepicker
provides a wide range of customization options. You can customize the appearance, date format, and behavior according to your requirements. Here are a few common customizations.
You can change the date format by modifying the dateFormat
prop.
<DatePicker
selected={selectedDate}
onChange={(date) => setSelectedDate(date)}
dateFormat="yyyy/MM/dd"
/>
You can apply custom styles to the date picker by wrapping it in a container with a specific class name and then defining CSS rules for that class.
<div className="custom-datepicker">
<DatePicker
selected={selectedDate}
onChange={(date) => setSelectedDate(date)}
/>
</div>
In your CSS file.
.custom-datepicker {
/* Custom styles here */
}
You can restrict date selection by setting the minDate
and maxDate
props.
<DatePicker
selected={selectedDate}
onChange={(date) => setSelectedDate(date)}
minDate={new Date()}
maxDate={new Date('2023-12-31')}
/>
In this article, we've covered the basics of adding a date picker to your React application using the react-datepicker
library. You learned how to install and import the library, create a simple date picker, and customize it to suit your needs. With this knowledge, you can now enhance the date selection experience in your React projects with ease
You might also like:
in this tutorial, we see how to get last 30 days record in laravel 8. You can simply get the last 30 days reco...
Feb-02-2022
In this post i will show you laravel 8 user role and permission with example, here we will see how to set user role and...
Jun-02-2021
In this article, we will see laravel 10 select2 autocomplete search using ajax. Here, we will learn about sele...
Apr-10-2023
In this example we will see autocomplete search using bootstrap typeahead js.Typeahead search is a method for progr...
Jun-09-2021