ITZone

React Datepicker Tutorial

Introduction

In this article we will learn how to use React datepicker in React application. Learn from basic to 1 case of swearing is a hotel booking component using React-Datepicker.

React-datepicker is the most popular library for React projects, which currently has over 700,000 donwloads.

Prequisities

In this tutorial you will need to know basic Javascript & React. The first step of the installation configuration steps create-React-app globally .

After create-react-app is installed, we will need to create new app with library. You can use the following cmd:

Then, after creating the application successfully, go to the project directory, install the required package and start the project.

Basic Implementation: Default

Before we start we need to put the CSS file in the application. Insert the following code into your application. You can basically put it anywhere in the app. In this application we will put in the app.js file

Then we import the DatePicker component from the react-datepicker . The events we will need are selected & onchange selected to denote the component value. While the onchange is working is a function that handles the selected value change. onChange function takes a value and we need to mistakenly change the value of the selected date. Update the App.js file as follows:

Basic Implementation: Time Picker

We can use the component time-picker by showTimeSelect props. Times will show about 30 minutes by default. We can we can specify how the date should show up according to dateFormat props. In this case we want to display in the component. Let’s use dateFormat="Pp" . We’re going to change App.js

Month & Year Picker

Aside from date & time, the ability to choose a month & year is important here. This component also has the option to choose month & year. Just make sure we use the correct dateFormat ddeerr format optimizing the App.js change features like this:

Advanced Example: Booking Hotel Date-Picker

We will create a date picker that sets the booking hotel date-picker component using react-datepicker. The goal is to build 2 react-datepicker components that get both connected as check-out dates, then display the selected dates. First all we need to do is create a component for check-in and check-out. We’ll use minDate props to set a minimum date limit for the user. Place this code inside your App.js file at the return section:

The way the code snippet above determines the minDate . For check-in component it simply uses current data as minDate , User cannot select date before today. And for the billing component, we can use it. checkInDate value, which means user cannot select 1 day prior to check-in date.

The next step will be to define how we will store the value in our state, and how each function’s behavior changes. For this, the main idea is to simply create a state for each checkInDate and checkOutDate value and store the date there. The only difference is minDate of check-out component will depend on checkInDate value, we need to re checkOutDate whenever checkIndate change. So you need to update App.js

The final part is that we need to display the summary of the reservation. It will only be displayed when both check-in and check-out dates are valid. For this case we use moment.js to format the date. Update the App.js file

The complete code App.js file

App.css styles file

Conclusion

Now we know how to use react-datepicker in our apps. After following the instructions in this article we can re-build it for further projects.

References

https://momentjs.com/docs/

https://www.npmjs.com/package/react-datepicker

https://codesource.io/complete-react-datepicker-tutorial-with-examples/

Share the news now