如何在 react native 中以 redux 形式设置隐藏字段?

How to set hidden fields in redux form in react native?

如何在 react native 中以 redux 形式设置隐藏字段?

我只是找不到任何方法来做到这一点。有帮助吗?

我遇到过同样的问题。 我所做的是像这样声明一个 Field 并将高度设置为 0。 这有点老套,但对我来说很管用。

<Field
    component={TextInput}
    name="departure_city_name"
    type="hidden"
    style={{ height: 0 }}
/>

然后我这样更改值:this.props.change("departure_city_name", cityData.name);

希望对您有所帮助。

我结束了使用这个:

this.props.dispatch(change("FORM_NAME","FIELD_NAME","VALUE")) 

此代码运行后,如果该字段不存在,表单将创建该字段

只要您需要 redux 形式的隐藏字段,您就不需要发送任何操作,只需执行此更改即可。

this.props.change('Field_name', value)

从技术上讲,您不需要创建 type=hidden 字段,因为如果字段不存在,您可以为要更改的 'hidden' 字段调用 change 函数 Redux-表单会将其添加到您的表单状态中,就像实际字段一样

MyForm = reduxForm({
  form: 'myForm'
})(MyForm)

MyForm = connect(
  state => ({
    initialValues: { areYouOk: 'no' }
  })
)(MyForm)

export default MyForm

无论您是否设置了初始值,您始终可以使用新的隐藏值调用更改函数,并且它无论如何都会起作用

  let MyForm = ({ submitHandler, change }) => {
    return (
      <form onSubmit={submitHandler}>
        <button
         onClick={() => {
           change('areYouOk', 'yes');
         }}
         label={'Yes'}
        />
      </form>
    );
  }