React-Final-Form 隐藏字段值

React-Final-Form hidden field values

我正在从 Redux-forms 过渡到 react-final-forms,但我无法弄清楚如何处理隐藏的表单值。在 redux-forms 中我可以做类似的事情 this.props.change("createdBy", this.props.user.profile.username) 设置隐藏值,但该选项在 react-final-forms 中似乎不是一个选项(或者我遗漏了它)。

我认为我可以使用 initialValues 来处理新的表单,但是如果我需要在初始表单中添加额外的值,我 运行 就会遇到麻烦。例如,

<Form
  onSubmit={ this.onSubmit }
  initialValues={
    item
      ? item
      : { createdBy: "Rachel", createdOn: new Date(), active: true }
  }
  render={({ handleSubmit, form, submitting, pristine, values }) => (
    <form onSubmit={handleSubmit}>
...

项目可能看起来像:

{
   code: "WTTC",
   description: "Welcome to the Club",
   active: true,
   createdBy: "Rachel",
   createdOn:"02/23/2021",
   modifiedBy: null,
   modifiedOn: null,
}

因此,如果我使用上述项目设置表单的初始值,我还想设置 modifiedBy 和 modifiedOn 值,我使用 this.props.change("createdBy", this.props.user.profile.username).

有什么建议吗?

我完全不熟悉这个库,但从 JS 的角度来看,你可以通过操作 item 对象的初始值来实现它

initialValues={
          item
            ? {...item,createdBy: this.props.user.profile.username, createdOn: new Date()}
            : { createdBy: "Rachel", createdOn: new Date(), active: true }
        }
              <Field name="fieldName">
                {({input}) => (
                  <div>
                    <div className="field-wrapper">
                      <input {...input} type="hidden" name="fieldName" value="1"/>
                    </div>
                  </div>
                )}
              </Field>

props.form.getFieldState('fieldName').change(value);

form 是来自

的最终形式的实例
 <Form
        onSubmit={onSubmit}
        subscription={{ submitting: true }}
        validate={validate}
        initialValues={initValues}
        render={({ handleSubmit, form, submitting, pristine, values }) => (