以编程方式更改Redux-Form字段值

时间:2017-07-21 06:29:57

标签: javascript reactjs redux redux-form

当我使用redux-form v7时,我发现无法设置字段值。现在在我的form中,我有两个select组件。当第一个select组件值发生变化时,第二个值将清晰。

在课堂渲染中:

<div className={classNames(style.line, style.largeLine)}>
  <div className={style.lable}>site:</div>
  <div className={style.content}>
    <Field
      name="site"
      options={sites}
      clearable={false}
      component={this.renderSelectField}
      validate={[required]}
    />
  </div>
</div>

<div className={classNames(style.line, style.largeLine)}>
  <div className={style.lable}>net:</div>
  <div className={style.content}>
    <Field
      name="net"
      options={nets}
      clearable={false}
      component={this.renderSelectField}
      validate={[required]}
      warning={warnings.net}
    />
  </div>
</div>

现在我添加select更改挂钩,如何更改其他select

renderSelectField = props => {
  const {
    input,
    type,
    meta: { touched, error },
    ...others
  } = props
  const { onChange } = input
  const _onChange = value => {
    onChange(value)
    this.handleSelectChange({ value, type: input.name })
  }
  return (
    <CHSelect 
      error={touched && error}
      {...input}
      {...others}
      onChange={_onChange}
      onBlur={null}
    />
  )
}

1 个答案:

答案 0 :(得分:24)

您可以在this.handleSelectChange({ value, type: input.name })中使用onChange逻辑并使用{​​{3}}

根据文件:

  

更改(字段:字符串,值:任意):功能

     

更改a的值   Redux商店中的字段。这是一个绑定的动作创建者,所以它   没有回报。

代码:

handleSelectChange = (value, type) => {
     if(type === "site") {
          this.props.change('net', "newValue");
     }
}
const mapDispatchToProps = (dispatch) => {
   return bindActionCreators({change}, dispatch);
}