26

I have redux form having dropdown and text fields. On dropdown change i have to update the other fields. The other fields are using custom component example

So the structure is like. 1. component having form 2. Field component.

Now i googled and found few things to update the field but not able to do it. What i tried 1. Adding state in the field like below in value. Does not work.

<Field name="accountno" value="this.state.accountno" component={InputText} placeholder="Number" />
  1. Tried disptach but not able to use it. Getting error for no dispatch i prop type

    this.props.dispatch(change('form', 'accountno', 'test value'));

  2. Tried this.props.fields but same not fields in props type.

It should work using 2 or 3 but not able to find where to add those. Please let me know how can i use these or any other way.

Rahul Tailwal
  • 3,183
  • 3
  • 14
  • 27
  • You should validate `onChange` as a property of `input` with `PropTypes.shape`. – Julio Betta May 02 '17 at 13:52
  • @juliobetta where should i add that? – Rahul Tailwal May 03 '17 at 04:56
  • WOW! This is a complete different question now =). well, IMHO, I don't think you should validate all `redux-form` methods and props (`onChange`, `disabled`, etc...) in your custom input component, unless they are required. But answering your question, take a look at this article http://andrewhfarmer.com/validate-nested-proptypes. It shows how to use the proptype `shape`. – Julio Betta May 03 '17 at 12:18

2 Answers2

34

When using redux-form, you don't need to set the value directly into the field. You can use the method initialize to populate your form. Or initialValues as a property of the object passed to redux to map state to props. The other way is to set the values individually using the function change from redux-form. In order to make it to work, you need to wrap your component with connect from react-redux. It embeds the dispatch method in your props.

import React, { Component } from 'react';
import { reduxForm, Field, change } from 'redux-form';
import { connect } from 'react-redux';

class Form extends Component {
  componentDidMount() {
    this.props.initialize({ accountno: 'some value here' });
    // set the value individually
    this.props.dispatch(change('myFormName', 'anotherField', 'value'));
  }

  render() {
    return (
      <form onSubmit={...}>
        ...
        <Field name="accountno" component={InputText} placeholder="Number" />
        <Field name="anotherField" component={InputText} />
      </form>
    )
  }
}

Form = reduxForm({ 
  form: 'myFormName' 
})(Form);

export default connect(state => ({ 
  // alternatively, you can set initial values here...
  initialValues: {
    accountno: 'some value here'
  } 
}))(Form);
Community
  • 1
  • 1
Julio Betta
  • 2,275
  • 1
  • 25
  • 25
  • i have tried all these options, none are working. i have it set like `SecurityExemptionsNew = connect(mapStateToProps, mapDispatchToProps)( SecurityExemptionsNew ); //Decorate the form component export default reduxForm({ form: 'SecurityExemptionsNewForm', // a unique name for this form initialValues: { service_domain: 'Corporate (Internal/External)' } })(SecurityExemptionsNew); ` – shorif2000 Mar 15 '18 at 15:13
  • ```change()``` worked for me better than initalValues becasue ```initialize``` rerenderd my entire page and made it useless where change only rerenderd that input. thanks julio – CDM social medias in bio Jul 28 '19 at 19:30
  • I've been looking for `change` option for many pages, thank you so much!!! – CrsCaballero Feb 20 '20 at 20:36
10

Just came up against this issue as well. The solution is to use initialValues as in juliobetta's answer, however the key is to set the value to a variable, such as state.blah.

You then need to set enableReinitialize to true so that the fields get updated when the state changes:

export default connect(state => ({ 
  initialValues: {
    accountno: state.accountno,
  },
  enableReinitialize: true,
}))(Form);

Now, every time you change state.accountno, the form field will be updated.

Malvineous
  • 25,144
  • 16
  • 116
  • 151
  • This has a drawback in that it will reset other form fields that the user may have modified back to their initial values; in other words you can't use it for both initial values and for setting some field values dynamically, and have to track the entire state inside `initialValues`. It just starts to feel like working against redux. Maybe there is a workaround. – Matt Montag Jan 29 '20 at 21:53