简体   繁体   English

如何在 React 中使用 react-date-range 发布请求日期?

[英]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.

相关问题 如何将按钮添加到反应日期范围 - How to add a button to react-date-range 如何使用webpack模块捆绑器实现react-date-range picker? - How to implement react-date-range picker with webpack module bundler? 反应日期范围创建自定义输入范围 - react-date-range create custom inputRanges 如何在 react-date-range 中的日期范围 select 之后自动关闭日期范围模式 - How to auto close date range modal after date range select in react-date-range 在 react-date-range 中识别选定的日期范围 - Identify selected date range in react-date-range 使用 react-date-range 进行日期选择的月份跳跃 - Month jumping on date selection using react-date-range 单击 react-date-range 定义的范围使用回调 - Using a callback on click of react-date-range defined ranges 如何更改 react-date-range 包中选择的输入颜色? - How I can change color of input selected in react-date-range package? 仅选择开始日期或结束日期时如何处理反应日期范围(日期范围)的 onChange 事件 - How to handel event onChange of react-date-range (Date range) when only select start date or end date 加载页面时,如何在默认情况下在 react-date-range npm 中在静态范围内显示选定的过去 30 天的值 - How to show selected last 30 days value in static range by default in react-date-range npm when page is load
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM