ReactJs 复选框在第一次使用后不会更改值

ReactJs Checkbox doesn't change value after first use

我有一个包含各种表单元素的大型表单,这些表单元素是根据获取请求动态呈现的。所有其他类型的表单(例如文本和 select)都工作正常,但复选框不是。

我勾选了一次之后,它就一直亮着(即使我取消勾选),我是不是遗漏了什么或者做错了什么?

这是我当前的相关代码:

class Input extends Component{
    render(){
        var form;
        if (this.props.componentClass=="choice") {
            // select form
        }

        else if (this.props.componentClass=="bool")
            form =(<Checkbox id={this.props.controlId} onChange={this.props.onChange}
                               defaultChecked={this.props.placeholder} >
                        </Checkbox>);
        else
            // text form
        return (
            <div>
                <Form inline onSubmit={this.handleSubmit}>
                    <FormGroup controlId={this.props.controlId}>
                        <ControlLabel>{this.props.name}</ControlLabel>
                        {form}
                        <Panel>
                        {this.props.description}
                        </Panel>
                        <FormControl.Feedback />
                    </FormGroup>
                </Form>
                <br/>
            </div>
        );
    }
}

// onChange code (comes from a parent component)
    onChange(e){
        const form = Object.assign({}, this.state.form);
        form[e.target.id] = e.target.value;
        this.setState({ form });
        console.log('current state: ', this.state);
    }

问题是您为复选框提供了 onChange 而未将其绑定到值。因此,初始检查正在工作,因为这就是 defaultChecked 为您所做的,但是一旦您实际与它交互,您就没有状态绑定到它导致反应不会在选中或未选中的位置重新呈现它。

您必须如前所述绑定 onChange 函数,但您应该使用 "checked" 而不是 "value"。

这是您这样修改的示例:

https://jsfiddle.net/8d3of0e7/3/

class Input extends React.Component{

    constructor(props){
    super(props)
    this.state = {form:{}}
  }

    render(){
    var form;
    if (this.props.componentClass=="choice") {
      // select form
    }else if (this.props.componentClass=="bool"){
      form = (
        <ReactBootstrap.Checkbox 
          id={this.props.controlId} 
          onChange={this.props.onChange.bind(this)}
          checked={this.state.form[this.props.controlId]}
          defaultChecked={this.props.placeholder} >
        </ReactBootstrap.Checkbox>);
    }else{
      // text form
    }
    return (
      <div>
        <ReactBootstrap.Form inline onSubmit={this.handleSubmit}>
          <ReactBootstrap.FormGroup controlId={this.props.controlId}>
            <ReactBootstrap.ControlLabel>
              {this.props.name}
            </ReactBootstrap.ControlLabel>
            {form}
            <ReactBootstrap.Panel>
              {this.props.description}
            </ReactBootstrap.Panel>
            <ReactBootstrap.FormControl.Feedback />
          </ReactBootstrap.FormGroup>
        </ReactBootstrap.Form>
        <br/>
      </div>
    );
  }

  componentDidUpdate(){
    console.log('current state: ', this.state);
  }

}

function onChange(e) {
  const form = Object.assign({}, this.state.form);
  form[e.target.id] = e.target.checked;
  this.setState({ form });
}

ReactDOM.render(
    <Input componentClass='bool' controlId='retired' 
    name='Is retired?' onChange={onChange}/>,
    document.getElementById('root')
)

在此示例中,我们的状态将是:state:{form:{retired:true}}