【问题标题】:setValue setting attribute to undefined react hook formsetValue 将属性设置为未定义的反应钩子形式
【发布时间】:2021-09-09 20:16:27
【问题描述】:

我在我的项目中使用 react-hook-form 我想分配一个字段以在某些条件下预填充但设置值无法正常工作 虽然它正确验证但没有设置值 这是我的表单域

<div className="item-input hal-input hal-margin-bottom-0 item-input-field not-empty-state">
  <select
    disabled={
      activeStep === 2 || activeStep === 3 || dataForDropdown?.length == 1
    }
    {...register("issuerAccount", {
      required: true,
    })}
    onChange={(e) => setNickname(e)}
    className="hal-input-select account-selector "
    data-type="account"
  >
    <option value="">- Select -</option>
    {dataForDropdown?.map((x, i) => (
      <option value={x.id} selected={dataForDropdown?.length == 1}>
        {x.id}
      </option>
    ))}
  </select>
  {errors.issuerAccount && (
    <span className="hal-validation_errors creditorAccount">
      {literals("newSubscription.field.requiredText")}
    </span>
  )}
</div>;

这是数据

useEffect(() => {
  if (issuer)
    setDataForDropdown(
      issuer.accounts.map((x) => ({
        id: x.accountNumber,
        text: x.branchName,
        selected: true,
      }))
    );

  if (issuer && issuer.accounts.length == 1) {
    console.log(issuer.accounts[0].accountNumber);
    setValue("issuerAccount", issuer.accounts[0].accountNumber);
  }
}, [issuer]);

我想设置 issuerAccount 字段,但我在提交表单时得到未定义

【问题讨论】:

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


    【解决方案1】:

    所以问题是我禁用了 select 元素,这就是它在表单对象上保存 undefined 的原因。

    【讨论】:

      猜你喜欢
      • 2021-10-15
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 2019-02-13
      • 2019-10-08
      相关资源
      最近更新 更多