我有一个注册表。当用户接受服务条款时,他单击一个按钮,选中该复选框。当他单击拒绝时,此按钮取消选中该复选框。
我读了this answer to programmatically change redux-form values。我可以更改除复选框以外的任何字段。
这是我的代码:
class RegisterContainer extends Component {
constructor(props) {
super(props);
this.state = {
modalTOS: false
};
this.accept = this.accept.bind(this);
this.decline = this.decline.bind(this);
this.toggleTOSModal= this.toggleTOSModal.bind(this);
}
accept() {
this.props.actions.change("register", "email", "foo42bar"); //This is a working test
this.props.actions.change("register", "read", true);
this.toggleTOSModal();
}
decline() {
this.props.actions.change("register", "read", false);
this.toggleTOSModal();
}
// ....
我添加此行只是为了检查是否可以更改电子邮件值:
this.props.actions.change("register", "email", "foo42bar");
有效!
一步一步,我尝试了很多选项进行检查,但没有人更改复选框:
this.props.actions.change("register", "read", "on");
this.props.actions.change("register", "read", true);
this.props.actions.change("register", "read", "1");
this.props.actions.change("register", "read", 1);
this.props.actions.change("register", "read", "true");
this.props.actions.change("register", "read", "a");
在验证器中,我添加了一些console.error
。通过手动检查或通过接受按钮,值是true。但是Checkbox不会选中
编辑:我的字段声明:
<Field name="read" component={FormCheckBoxGroup} {...fieldProps} onClick={onClickTos} required />
FormCheckBoxGroup:
<FormGroup check>
<Col sm={{ size: 8, offset: 4 }}>
<Label check className={className}>
<Input {...input} type="checkbox" disabled={disabled} required={required} />
{label}
</Label>
{!required && helpBlock && <HelpBlock>{helpBlock}</HelpBlock>}
{error && <HelpBlockErrors errors={messages} />}
{children}
</Col>
</FormGroup>
有想法吗?
答案 0 :(得分:1)
true / false仅在您告诉redux-form它是一个复选框时才起作用。来自文档:
input.checked:布尔值[可选] 仅当value为布尔值时,才是value的别名。提供了将整个字段对象分解为表单元素的便利。
请确保您正确声明了Field
<Field name="accpetTerms" component="input" type="checkbox" />