2017-07-21 1 views
4

redux-form v7を使用すると、フィールド値を設定する方法がないことがわかります。今私のformには2つのselectコンポーネントがあり、最初のselectコンポーネント値が変更されたときに2番目の値がクリアされます。クラスのプログラムでRedux-Formフィールド値を変更する

レンダリング:

<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 changeフックを追加し、どのように私は他の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} 
      /> 
     ) 
    } 

答えて

8

を変更することができますあなたはthis.handleSelectChange({ value, type: input.name })でのonChangeロジックを持っているとchange action from redux-formを使用することができます

文書によれば、

変化(フィールド:文字列値:任意):関数

はReduxのストア内 フィールドの値を変更します。これはバインドされたアクション作成者なので、 は何も返しません。

コード:

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

おかげで多く、それが – taven

+0

が、同時に複数のフィールドを設定するためのAPIがある素晴らしいですか! initialValues中ではない –

関連する問題