Programmatically change Redux-Form Field value

55,029

You can have the onChange logic in this.handleSelectChange({ value, type: input.name }) and use change action from redux-form

According to the docs:

change(field:String, value:any) : Function

Changes the value of a field in the Redux store. This is a bound action creator, so it returns nothing.

Code:

import { change } from "redux-form";

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

const mapDispatchToProps = (dispatch) => {
  return bindActionCreators({change}, dispatch);
}
Share:
55,029

Related videos on Youtube

taven
Author by

taven

Updated on July 09, 2022

Comments

  • taven
    taven almost 2 years

    When I use redux-form v7, I find there is no way to set the field value. Now in my form, I have two select component. The second's value will be clear when the first select component value changed.

    In class render:

    <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>
    

    Now I add the select change hook, and how can I change the other select value

    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}
        />
      )
    }
    
  • Alexandre Leites
    Alexandre Leites about 6 years
    is there an API to set multiple fields at the same time? not during initialValues
  • Dane
    Dane about 6 years
    how does this work for the component='select' variant of <Field> component ?
  • Shubham Khatri
    Shubham Khatri about 6 years
    @Dane similar to how it would work for the input field, it need the field name as the first parameter and the option's value that you need to select
  • shinzou
    shinzou about 6 years
    Can you explain a little what happens here? why bind dispatch to change? and how change is a props method?
  • Shubham Khatri
    Shubham Khatri about 6 years
    @shinzou, mapDispatchToProps is something that you use with connect, any action creator that needs to be called from the component need to be bind with dispatch. You can read more about it stackoverflow.com/questions/49994197/… and github.com/reactjs/react-redux/blob/master/docs/api.md
  • SpiXel
    SpiXel about 6 years
    @ShubhamKhatri where is the change function imported from? Do we really need to bind it to component props or reduxForm automatically does that? in your code : bindActionCreators({change}, dispatch); , {change} is obviously undefined and should be imported from somewhere right ?
  • Alexandre Tranchant
    Alexandre Tranchant over 5 years
    import { change } from "redux-form";
  • kheengz
    kheengz almost 5 years
    NOTE: the change from redux-form it's the function that does the trick (upating)
  • Maddocks
    Maddocks almost 5 years
    dispatch(change({someField: someValue})
  • rotimi-best
    rotimi-best over 4 years
    If you import change from redux-form, then the arguments are: change(form: string, field: string, value: any, touch?: boolean, persistentSubmitErrors?: boolean): FormAction
  • kunambi
    kunambi almost 4 years
    @MichaelBuen I solved it by setting the keepDirtyOnReinitialize: true setting in order to affect all fields