当单击React Flatpickr上的按钮时如何获取值?

huangapple go评论106阅读模式
英文:

How to get value when click button on react flatpickr?

问题

我在React中有一个组件,并附加了react-flatpicker以获取日期值。但是,在我使用useRef后,它显示值“可能为null”,是的,在console.log之后,结果是相同的:

  1. <Flatpickr
  2. ref={fp}
  3. name="startTime"
  4. className="py-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
  5. options={{enableTime: true, noCalendar: true, dateFormat: "H:i",}}
  6. />

这是函数:

  1. const fp = useRef(null);
  2. function handleSubmitAgenda() {
  3. console.log(fp?.current?.value);
  4. }

请告诉我关于这个的详细信息。

英文:

I have a component in React and attach react-flatpicker to get date value.
However, after I use useRef, it shows that value is "possibly null" and yes after console.log it is the same result:

  1. &lt;Flatpickr
  2. ref={fp}
  3. name=&quot;startTime&quot;
  4. className=&quot;py-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500&quot;
  5. options={{enableTime: true, noCalendar: true, dateFormat: &quot;H:i&quot;,}}
  6. /&gt;

And here is the function:

  1. const fp = useRef(null);
  2. function handleSubmitAgenda() {
  3. console.log(fp?.current?.value);
  4. }

Please enlighten me about this.

答案1

得分: -1

要在单击按钮时从React Flatpickr组件获取值,您需要使用Flatpickr提供的onChange属性。

请查看下面的代码库。

  1. import { useState, useRef } from "react";
  2. import Flatpickr from "react-flatpickr";
  3. function YourComponent() {
  4. const [selectedDateTime, setSelectedDateTime] = useState(null);
  5. const fp = useRef(null);
  6. // 用于处理日期/时间选择更改的函数
  7. const handleDateTimeChange = (selectedDates) => {
  8. setSelectedDateTime(selectedDates[0]);
  9. };
  10. function handleSubmitAgenda() {
  11. console.log(selectedDateTime); // 单击按钮时将记录所选的日期/时间
  12. }
  13. return (
  14. <div>
  15. <Flatpickr
  16. ref={fp}
  17. name="startTime"
  18. className="py-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
  19. options={{
  20. enableTime: true,
  21. noCalendar: true,
  22. dateFormat: "H:i",
  23. }}
  24. onChange={handleDateTimeChange} // 在日期/时间选择更改时调用handleDateTimeChange
  25. />
  26. <button onClick={handleSubmitAgenda}>Submit</button>
  27. </div>
  28. );
  29. }

希望这对您有所帮助。祝一切顺利。

英文:

To get the value from the React Flatpickr component when you click a button, you need to use the onChange prop provided by Flatpickr.

Please check the codebase below.

  1. import { useState, useRef } from &quot;react&quot;;
  2. import Flatpickr from &quot;react-flatpickr&quot;;
  3. function YourComponent() {
  4. const [selectedDateTime, setSelectedDateTime] = useState(null);
  5. const fp = useRef(null);
  6. // Function to handle the date/time selection change
  7. const handleDateTimeChange = (selectedDates) =&gt; {
  8. setSelectedDateTime(selectedDates[0]);
  9. };
  10. function handleSubmitAgenda() {
  11. console.log(selectedDateTime); // This will log the selected date/time when the button is clicked
  12. }
  13. return (
  14. &lt;div&gt;
  15. &lt;Flatpickr
  16. ref={fp}
  17. name=&quot;startTime&quot;
  18. className=&quot;py-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500&quot;
  19. options={{
  20. enableTime: true,
  21. noCalendar: true,
  22. dateFormat: &quot;H:i&quot;,
  23. }}
  24. onChange={handleDateTimeChange} // Call handleDateTimeChange on date/time selection change
  25. /&gt;
  26. &lt;button onClick={handleSubmitAgenda}&gt;Submit&lt;/button&gt;
  27. &lt;/div&gt;
  28. );
  29. }

I hope this will be helpful for you.
Best.

huangapple
  • 本文由 发表于 2023年8月4日 08:50:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76832340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定