[英]How to post request dates with react-date-range in React?
I am relatively new to react and wanted to use the react-date-range
library.我对反应比较陌生,想使用
react-date-range
库。 For this, I want to send a POST
request with axios
the start date and the end date.为此,我想发送一个带有
axios
开始日期和结束日期的POST
请求。 However, I am having problems with combining the library and the post request.但是,我在组合库和发布请求时遇到问题。 Can somebody help me with this problem?
有人可以帮我解决这个问题吗? Because now the start dates and end dates are not submitted and I don't know what to do.
因为现在开始日期和结束日期没有提交,我不知道该怎么办。
import React, { Component, useState } from 'react';
import './Datepickersearch.css';
import 'react-date-range/dist/styles.css'; // main style file
import 'react-date-range/dist/theme/default.css'; //theme css file
import { DateRangePicker } from 'react-date-range';
import { Button } from '@material-ui/core';
import { useHistory } from 'react-router-dom';
import axios from 'axios';
function Search() {
const history = useHistory();
const [startDate, setStartDate] = useState(new Date());
const [endDate, setEndDate] = useState(new Date());
const selectionRange = {
startDate: startDate,
endDate: endDate,
key: 'selection',
};
function handleSelect(ranges) {
setStartDate(ranges.selection.startDate);
setEndDate(ranges.selection.endDate);
}
axios.post(`http://localhost:8080/api/reservation/`, { startDate, endDate }).then((res) => {
console.log(res);
console.log(res.data);
});
return (
<div className="datepickersearch">
<DateRangePicker ranges={[selectionRange]} onChange={handleSelect} />
<h2> Number of guests</h2>
<input min={0} defaultValue={2} max={7} type="number" />
<Button onClick={() => history.push('/search')}>Search Apartments</Button>
</div>
);
}
export default Search;
You need to add onClick method on button, and call a method to post the data, dates are getting populated(check console statements).您需要在按钮上添加 onClick 方法,并调用一个方法来发布数据,日期正在填充(检查控制台语句)。
Please check this code - https://codesandbox.io/s/axios-post-data-dhded?file=/src/Search.jsx请检查此代码 - https://codesandbox.io/s/axios-post-data-dhded?file=/src/Search.jsx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.