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

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

How to get value when click button on react flatpickr?

问题

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

<Flatpickr
    ref={fp}
    name="startTime"
    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"
    options={{enableTime: true, noCalendar: true, dateFormat: "H:i",}}
/>

这是函数:

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

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

英文:

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:

&lt;Flatpickr
    ref={fp}
    name=&quot;startTime&quot;
    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;
    options={{enableTime: true, noCalendar: true, dateFormat: &quot;H:i&quot;,}}
/&gt;

And here is the function:

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

Please enlighten me about this.

答案1

得分: -1

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

请查看下面的代码库。

import { useState, useRef } from "react";
import Flatpickr from "react-flatpickr";

function YourComponent() {
  const [selectedDateTime, setSelectedDateTime] = useState(null);
  const fp = useRef(null);

  // 用于处理日期/时间选择更改的函数
  const handleDateTimeChange = (selectedDates) => {
    setSelectedDateTime(selectedDates[0]);
  };

  function handleSubmitAgenda() {
    console.log(selectedDateTime); // 单击按钮时将记录所选的日期/时间
  }

  return (
    <div>
      <Flatpickr
        ref={fp}
        name="startTime"
        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"
        options={{
          enableTime: true,
          noCalendar: true,
          dateFormat: "H:i",
        }}
        onChange={handleDateTimeChange} // 在日期/时间选择更改时调用handleDateTimeChange
      />

      <button onClick={handleSubmitAgenda}>Submit</button>
    </div>
  );
}

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

英文:

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.

import { useState, useRef } from &quot;react&quot;;
import Flatpickr from &quot;react-flatpickr&quot;;

function YourComponent() {
  const [selectedDateTime, setSelectedDateTime] = useState(null);
  const fp = useRef(null);

  // Function to handle the date/time selection change
  const handleDateTimeChange = (selectedDates) =&gt; {
    setSelectedDateTime(selectedDates[0]);
  };

  function handleSubmitAgenda() {
    console.log(selectedDateTime); // This will log the selected date/time when the button is clicked
  }

  return (
    &lt;div&gt;
      &lt;Flatpickr
        ref={fp}
        name=&quot;startTime&quot;
        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;
        options={{
          enableTime: true,
          noCalendar: true,
          dateFormat: &quot;H:i&quot;,
        }}
        onChange={handleDateTimeChange} // Call handleDateTimeChange on date/time selection change
      /&gt;

      &lt;button onClick={handleSubmitAgenda}&gt;Submit&lt;/button&gt;
    &lt;/div&gt;
  );
}

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:

确定