redux-form-material-ui icon indicating copy to clipboard operation
redux-form-material-ui copied to clipboard

Setting a value on a selectField

Open jmsherry opened this issue 8 years ago • 1 comments

OK, I've read the readme, so I know that defaultValue won't work but I can't set a value on the selectField even using the value prop:


<Field
  name={`service_${index}`}
  component={SelectField}
  hintText="Select a service"
  value={0} // <-- here
  children={serviceOptions}
/>

Is that expected behaviour?

(serviceOptions look like:)

const serviceOptions = services.map(serviceOption => (
    <MenuItem
      key={serviceOption.id}
      value={serviceOption.id} // <-- first one will be 0
      primaryText={serviceOption.name}
      style={{
        textAlign: 'left',
      }}
    />
  ));

I thought that if the value on the SelectField matched a value on a MenuItem, then it would select that item? Am I wrong about that??

Thanks

jmsherry avatar Jul 10 '17 00:07 jmsherry

Redux form has a special way of handling controlled component's initial state. You must use the reduxForm HOC's initialValues field instead.

reduxForm({
    name: 'myForm',
    initialValues: {
        'service_1': 0
    }
})(MyFormComponent)

credli avatar Dec 11 '17 16:12 credli