【问题标题】:react final form conditional form fields are not getting values while rendered反应最终形式条件表单字段在渲染时未获取值
【发布时间】:2019-09-12 08:24:51
【问题描述】:

我想根据另一个字段的值呈现表单中的一个字段。

我监听字段的“OnChange”事件,然后触发新字段的form.change(还没有渲染),但是渲染的时候取不到值。

我为此问题创建了一个沙盒:https://codesandbox.io/embed/priceless-keldysh-gfd3b

预期结果:选择“加热”时,应显示比例因子并得到值'1'

解决这个问题的最佳做法是什么?假设表单中有很多依赖项。

【问题讨论】:

    标签: reactjs react-final-form final-form


    【解决方案1】:

    因为<Form /> 需要可见的<Field /> 才能订阅

    但是当 values.meterType !== "heat" 时,您的字段 scalingFactor 将被删除

    {values.meterType && values.meterType === "heat" && (
      <Field name="scalingFactor" component="input" />
    )}
    

    你需要修改你的代码如下:

    <Field name="scalingFactor">
      {({ input }) => {
        return (
          <React.Fragment>                    
            {values.meterType && values.meterType === "heat" &&
              <input {...input} />
            }
          </React.Fragment>
        );
      }}
    </Field>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 1970-01-01
      • 2022-01-17
      • 2019-03-25
      • 2021-05-01
      相关资源
      最近更新 更多