简体   繁体   English

反应最终形式条件形式字段在渲染时未获取值

[英]react final form conditional form fields are not getting values while rendered

I have a field in a form that I want to render based on another field's value. 我有一个要根据另一个字段的值呈现的表单中的字段。

I listen to "OnChange" event of the field, and then trigger form.change of the new field (which is not rendered yet), but it won't get the value while rendered. 我监听该字段的“ OnChange”事件,然后触发新字段的form.change(尚未渲染),但在渲染时不会获取该值。

I created a sandbox for the issue: https://codesandbox.io/embed/priceless-keldysh-gfd3b 我为此问题创建了一个沙箱: https : //codesandbox.io/embed/priceless-keldysh-gfd3b

THe expected result: when selecting "Heat", scaling factor should be displayed and get the value '1' 预期结果:选择“加热”时,应显示比例因子并获得值“ 1”

What is the best practice to solve that? 解决该问题的最佳做法是什么? assuming there are a lot of dependencies inside a form. 假设表单内有很多依赖关系。

Since <Form /> need visible <Field /> for subscribe 由于<Form />需要可见的<Field />进行订阅

but your field scalingFactor would be removed when values.meterType !== "heat" 但是当values.meterType!==“ heat”时,您的字段scaleFactor将被删除

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

You need to change your code as bellow: 您需要将以下代码更改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM