【问题标题】:react-hook-form not triggering onSubmit when using Controller使用 Controller 时 react-hook-form 未触发 onSubmit
【发布时间】:2021-11-02 19:51:05
【问题描述】:

我有以下表格:

      <form onSubmit={handleSubmit(onSubmit)}>
        <Controller
          name="voucherPrice"
          control={control}
          defaultValue={false}
          rules={{ required: true }}
          render={({ field: { onChange, value, ref } }) => (
            <Input {...field} onChange={(ev) => handlePriceInputChange(ev)} value={price} type="number" innerRef={ref} />
          )}
        />

        <p>{errors.voucherPrice?.message}</p>

        <Button
          variant="contained"
          sx={{ mt: 1, mr: 1 }}
          type="submit"
        >
          {"Continue"}
        </Button>
      </form>

并使用此配置:

function PriceSelection(props) {
  const {
    register,
    handleSubmit,
    control,
    formState: { errors },
  } = useForm({
    resolver: yupResolver(schema),
  });
  const onSubmit = (data) => {
    console.log("does not work?", data);
  };

  const classes = useStylesPriceSelection();
  const [selected, setSelected] = useState(false);
  const [price, setPrice] = useState("");


  const handlePriceInputChange = (ev) => {
    console.log("change", price);
    setPrice(parseInt(ev.target.value));
  };



当我按下提交按钮时,函数onSubmit 没有触发。此外,我希望输入字段默认填充状态price,并且当我按下提交按钮时,它的值将与函数onSubmit 上的参数data 一起发送。

【问题讨论】:

  • 你不想在 onSubmit 中调用你的 handleSubmit 函数,而不是你想做的 onSubmit={handleSubmit(onSubmit)} onSubmit={() =&gt; handleSubmit(onSubmit)}。你到底想做什么?因为您正在尝试 console.log data 但您没有将任何数据传递到函数中。
  • 用那个控制台日志我只是想看看数据是否通过

标签: reactjs react-hooks react-hook-form yup


【解决方案1】:

您将useStatereact-hook-form 混合在一起,并且没有更新react-hook-form 的内部表单状态。您无需为您的字段声明useState

在您的示例中,您正在从&lt;Controller /&gt;field 对象中解构onChange,但您从未将它用于您的&lt;Input /&gt; 组件。因此react-hook-form 不能更新它的表单状态。当您将字段设置为必填时,不会触发 onSubmit 回调,因为 react-hook-form 永远不会收到它的更新或值。

正确的方法是:

<Controller
  name="voucherPrice"
  control={control}
  rules={{ required: true }}
  render={({ field: { ref, onChange, value, ...field } }) => (
    <Input {...field} onChange={onChange} value={value} type="number" innerRef={ref} />
  )}
/>

甚至更短:

<Controller
  name="voucherPrice"
  control={control}
  rules={{ required: true }}
  render={({ field: { ref, ...field } }) => (
    <Input {...field} type="number" innerRef={ref} />
  )}
/>

更新

如果您需要访问&lt;Controller /&gt; 之外的值,您应该使用react-hook-formwatch 方法。这将允许您订阅 voucherPrice 字段的最新值并在组件中使用它 -> Docs

如果您想以编程方式设置或更新值,您可以使用来自react-hook-formsetValue 方法 -> Docs

const { control, handleSubmit, watch, setValue } = useForm();

const voucherPrice = watch("voucherPrice");

const onButtonClick = () => {
  setValue("voucherPrice", <newValue>);
}

如果您确实需要为您的值设置一个单独的 useState 并希望将其另外更新为您的 react-hook-form 字段更新,您可以执行以下操作:

<Controller
  name="voucherPrice"
  control={control}
  rules={{ required: true }}
  render={({ field: { ref, onChange, value, ...field } }) => (
    <Input
      {...field}
      onChange={(v) => {
        onChange(v);
        handlePriceInputChange(v);
      }}
      value={value}
      type="number"
      innerRef={ref}
    />
  )}
/>

但我建议仅使用 react-hook-form 解决方案,因为它具有管理表单状态所需的所有功能。

【讨论】:

  • 问题是,我需要 onChange 函数和状态,以便它可以一直显示在“值”中(值也由按钮触发的函数设置,这表明价格用户)
  • 所以在这个例子中,我错过了那个功能
  • 我更新了答案以符合您的要求。
  • 谢谢,就是这样
猜你喜欢
  • 2021-07-28
  • 2023-01-31
  • 2020-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-18
  • 2022-06-22
  • 2021-10-05
相关资源
最近更新 更多