Mã javascript đặt phòng khách sạn

Trong bài viết này chúng ta sẽ học cách sử dụng React datepicker trong ứng dụng React. Tìm hiểu từ cơ bản đến 1 trường hợp thề là

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
4 sử dụng React-Datepicker

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
5 là thư viện phổ biến nhất cho các dự án React, thư viện này hiện có khoảng trên 700. 000 lượt tải xuống

điều kiện tiên quyết

Trong hướng dẫn này, bạn cần biết Javascript & React cơ bản. Bước đầu tiên của bước cấu hình là cài đặt ứng dụng tạo-phản ứng

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
6

npm i -g create-react-app

Sau đó

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
7 đã được cài đặt, chúng ta sẽ cần tạo ứng dụng mới cho thư viện. You can use cmd after

npx create-react-app datepicker-app

Sau đó, sau khi tạo ứng dụng thành công, hãy vào thư mục dự án, cài đặt gói yêu cầu và bắt đầu dự án

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start

Thực hiện cơ bản. Mặc định

Trước khi bắt đầu, chúng ta cần đặt tệp CSS trong ứng dụng. Chèn mã sau vào ứng dụng của bạn. Về cơ bản bạn có thể đặt bất kỳ nơi nào trong ứng dụng. In this application we will set in file app. js

// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";

Sau đó chúng ta nhập DatePicker component từ

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
8. Các sự kiện mà chúng ta cần phải có là
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
9 &
// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
0
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
9 lấy giá trị ngày tháng để biểu thị thành phần giá trị. Trong khi
// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
0 hoạt động là 1 hàm xử lý thay đổi giá trị
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
9. Hàm
// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
4 lấy giá trị và chúng ta cần thu thập là thay đổi giá trị của ngày
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
9 được chọn. Cập nhật file ứng dụng. js as after

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
6

Thực hiện cơ bản. Bộ chọn thời gian

Chúng ta có thể sử dụng thành phần

// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
6 bởi đạo cụ
// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
7. Times will show the interval 30 minutes by default. Chúng ta có thể chúng ta có thể định dạng ngày sẽ hiển thị như thế nào theo đạo cụ dateFormat. Trong trường hợp này, chúng tôi muốn hiển thị trong thành phần. Sử dụng ký hiệu
// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
8. Chúng ta sẽ thay đổi ứng dụng. js

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
0

Công cụ chọn tháng và năm

Ngoài ngày & giờ, khả năng chọn tháng & năm là một điều quan trọng cần có ở đây. thành phần này cũng có tính năng chọn tháng & năm. Chỉ cần đảm bảo rằng chúng ta sử dụng đúng định dạng

// import required react-datepicker styling file
import "react-datepicker/dist/react-datepicker.css";
9 ddeerr tối ưu các tính năng thay đổi ứng dụng. js as after

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
2

Ví dụ nâng cao. Đặt phòng khách sạn chọn ngày

Chúng ta sẽ tạo một bộ chọn ngày đặt thành phần công cụ chọn ngày đặt phòng khách sạn bằng cách sử dụng react-datepicker. Mục tiêu là xây dựng 2 thành phần

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
8 làm cho cả 2 được kết nối với ngày nhận phòng, sau đó hiển thị các ngày đã chọn. Đầu tiên tất cả những gì chúng ta cần làm là tạo thành phần để nhận phòng và trả phòng. Chúng ta sẽ sử dụng đạo cụ
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
61 để đặt giới hạn ngày tối thiểu cho người dùng. Đặt mã này bên trong tệp
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
62 của bạn tại phần trả lại

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
6

Cách mã hóa xác định trên

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
61. Đối với thành phần đăng ký, nó chỉ đơn giản là sử dụng dữ liệu hiện đang làm
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
61, Người dùng không thể chọn ngày trước ngày hôm nay. Và đối với thành phần thanh toán, chúng ta có thể xử lý. Giá trị
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
65, có nghĩa là người dùng không thể chọn 1 ngày trước ngày nhận phòng

Bước tiếp theo sẽ là xác định cách chúng ta sẽ lưu giá trị vào trạng thái của mình và cách hoạt động của mỗi chức năng thay đổi. Đối với điều này, tưởng tượng chính chỉ là cần tạo một trạng thái cho mỗi

// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
65 và
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
67 iá trị và lưu vào ngày đó. Sự khác biệt duy nhất là
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
61 của thành phần trả phòng sẽ phụ thuộc vào giá trị của
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
65, chúng ta cần phải thiết lập lại
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
67 bất cứ khi nào
// go to the project folder
cd datepicker-app

// install the required package
npm i --save react-datepicker
npm i --save moment

// start the project
npm start
01 thay đổi. Vì vậy bạn cần cập nhật ứng dụng. js

npm i -g create-react-app
6

The end of the end of the end of the we have to display the brief of the room. Nó sẽ chỉ được hiển thị khi cả ngày nhận phòng và trả phòng đều có giá trị. Đối tượng hợp trường hợp này chúng ta sử dụng thời điểm. js to format date. Cập nhật file ứng dụng. js

npm i -g create-react-app
7

Code hoàn chỉnh file App. js

npx create-react-app datepicker-app
0

Kiểu tệp ứng dụng. css

npx create-react-app datepicker-app
1

Phần kết luận

Bây giờ chúng ta đã biết cách sử dụng Reac-datepicker trong ứng dụng của mình. Sau khi làm theo hướng dẫn của bài viết này, chúng ta có thể sử dụng tái xây dựng cho các dự án tiếp theo