React redux Form checkbox `defaultChecked` not wor

2019-08-25 05:10发布

问题:

<Field
   defaultChecked={true}
   onChange={this.handleFormItemRadio}
   component={'input'}
   type={'checkbox'}
    name="tadmin" />

When the field is initialized, i expect it to be checked but it comes empty yet aim supplying in true value.

poited to number of solutions but am still not able to solve this.

回答1:

According to the docs, the prescribed way is to set initialValues

<Field
   onChange={this.handleFormItemRadio}
   component='input'
   type='checkbox'
   name="tadmin" />

...

export default reduxForm({
  form: 'simple', // a unique identifier for this form
  initialValues: { tadmin: true },
})(SimpleForm);

Here's a CodeSandbox example