简体   繁体   English

在反应日期选择器上使用钩子

[英]Using hooks on react date picker

I'm created a contact form that sends the data to an email and I want to be able to use the react date picker.我创建了一个将数据发送到 email 的联系表单,我希望能够使用反应日期选择器。 I've successfully managed to get all other input fields to save in the state and send using NodeMailer however the date doesn't have a name assigned to it, like the other (example: fullName) so I'm not sure how to add it on so I can easily retract in the backend like I'm doing for the other fields (req.body.fullName).我已经成功地将所有其他输入字段保存在 state 中并使用 NodeMailer 发送,但是日期没有分配给它的名称,就像另一个一样(例如:fullName)所以我不知道如何添加开启它,这样我就可以像在其他字段(req.body.fullName)中所做的那样轻松地在后端收回。 Appreciate any help:)感谢任何帮助:) 在此处输入图像描述

  const [startDate, setStartDate] = useState(new Date());

  <DatePicker
    selected={startDate}
    onChange={(date) => setStartDate(date)}
    inline
   />

You are probably checking the states using React Browser Extension.您可能正在使用 React Browser Extension 检查状态。
There you will see something like this:在那里你会看到这样的东西:

State: xyz
State: abc

It doesn't display the name of the state only shows the value.它不显示 state 的名称,只显示值。 Say you initialized two states:假设您初始化了两个状态:

const [a, setA] = useState(1);
const [b, setB] = useState({'var1': 'val1', 'var2':'val2'});

Using React extension you can see:使用 React 扩展你可以看到:

State: 1
State: {'var1': 'val1', 'var2':'val2'}

But these both have the name associated with it a and b respectively.但是它们都有分别与之关联的名称ab You can access these by their names only.您只能通过它们的名称访问它们。 So, here your state name is startDate .所以,这里你的 state 名字是startDate Try:尝试:

console.log(startDate)

Your date will be printed.您的日期将被打印出来。 And send it in the body of the API call.并将其发送到 API 调用的正文中。 And you can access it on the backend using req.body.startDate .您可以使用req.body.startDate在后端访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM