React Redux Form复选框`defaultChecked`不起作用 [英] React redux Form checkbox `defaultChecked` not working

查看:535
本文介绍了React Redux Form复选框`defaultChecked`不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

初始化该字段时,我希望可以对其进行检查,但是即使我提供了true值,它也仍处于未选中状态.

When the field is initialized, I expect it to be checked but it renders unchecked even though I am supplying a true value.

有人指出我有很多解决方案,但仍然无法解决.

I was pointed to number of solutions but am still not able to solve this.

推荐答案

根据

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

这是一个CodeSandbox 示例

Here's a CodeSandbox example

这篇关于React Redux Form复选框`defaultChecked`不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆