我有一个表单,我正在try 添加一个react 日期时间 Select 器,但我也试图将它与react-hook-form集成,以便它与表单的其余部分一起工作,但它似乎根本不起作用.

当表单第一次呈现时,它不会在日期 Select 器中显示当前日期,它只会显示-而且当我从弹出窗口中 Select 一个日期时,它不会在字段中更改.

import { Controller, useForm } from 'react-hook-form'
import DateTimePicker from 'react-datetime-picker'
import 'react-datetime-picker/dist/DateTimePicker.css'
import 'react-calendar/dist/Calendar.css'

const Jobs = () => {
    const { control, register, handleSubmit, watch, formState: { errors } } = useForm();

    const onSubmit = handleSubmit(async ({ firstname, startdate }) => {
        console.log(startdate)
    })

    return (
        <form onSubmit={handleSubmit(onSubmit)}>

            <div>
                <input
                    id="firstname"
                    name="firstname"
                    type="text"
                    { ...register("firstname") }
                />
            </div>

            <div>
                <Controller
                    control={control}
                    name='startdate'
                    render={({ field: { onChange, value } }) => (
                        <DateTimePicker
                            onChange={onChange}
                            selected={value}
                        />
                    )}
                />
            </div>
        </form>
    )
}

推荐答案

看起来您使用REACT-HOOK-FORM和REACT-DATETIME-PICKER是正确的.您面临的问题可能与日期字段的初始值设置不正确有关.

要解决这个问题,你可以set the initial value for the date field using the defaultValue prop in the Controller.此外,您应该确保传递给DateTimePicker的值是一个Date对象,因为Reaction-DateTime-Picker需要一个日期值.

以下是更新后的代码:

import { Controller, useForm } from 'react-hook-form';
import DateTimePicker from 'react-datetime-picker';
import 'react-datetime-picker/dist/DateTimePicker.css';
import 'react-calendar/dist/Calendar.css';

const Jobs = () => {
  const {
    control,
    register,
    handleSubmit,
    formState: { errors },
  } = useForm();

  const onSubmit = handleSubmit(async ({ firstname, startdate }) => {
    console.log(startdate);
  });

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <div>
        <input id="firstname" name="firstname" type="text" {...register('firstname')} />
      </div>

      <div>
        <Controller
          control={control}
          name="startdate"
          defaultValue={new Date()} // Set the initial value to the current date
          render={({ field: { onChange, value } }) => (
            <DateTimePicker onChange={onChange} value={value ? new Date(value) : null} />
          )}
        />
      </div>
    </form>
  );
};

通过将defaultValue设置为new Date(),日期选取器现在应该显示窗体第一次呈现时的当前日期.另外,确保传递给DateTimePicker的值是使用新日期(值)的Date对象.

Javascript相关问答推荐

如何expose 像React在onChange、onClick等中所做的那样的参数?

React状态变量在使用++前置更新时引发错误

Vue Quill css仅应用于我的第一个Quill Editor组件+如何自定义工具栏

为什么在获取回调内设置状态(不会)会导致无限循环?

为什么getRecord()会因为与_logger相关的错误而失败?(使用Hedera SDK)

确定MutationRecord中removedNodes的索引

字节数组通过echo框架传输到JS blob

我可以使用CSS有效地实现最大宽度=100%和最大高度=100%,而无需父母有明确的/固定的宽度和高度,替代方法吗?

如何找出摆线表面上y与x相交的地方?

将自定义排序应用于角形数字数组

对网格项目进行垂直排序不起作用

将数组扩展到对象中

在VS代码上一次设置多个变量格式

将多个文本框中的输出合并到一个文本框中

不同表的条件API端点Reaction-redux

如何使用画布在另一个内部绘制一个较小但相同的形状,同时保持恒定的边界厚度?

背景动画让网站摇摇欲坠

MongoDB通过数字或字符串过滤列表

在没有任何悬停或其他触发的情况下连续交换图像

material UI自动完成全宽