React redux Form checkbox `defaultChecked` not working

a 夏天 提交于 2019-12-11 10:41:52

问题


<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



来源:https://stackoverflow.com/questions/54872817/react-redux-form-checkbox-defaultchecked-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!