简体   繁体   English

无法获得正确的复选框状态

[英]Cannot get correct state of checkbox

Something weird is happening. 奇怪的事情正在发生。 Im using foundation switch as my checkbox. 我使用基础开关作为我的复选框。

When I look at my state on the react browser tool, my check state is true. 当我在react浏览器工具上查看状态时,我的检查状态为true。 When I console log the state in the handler, it's false. 当我在处理程序中控制台记录状态时,它是错误的。 Where is the bug? 错误在哪里?

es6: es6:

constructor(props){
  super(props);
  this.state = {
    checked: false
  },
  this._handleChange = this._handleChange.bind(this)
}

_handleChange(){
  this.setState({ checked: !this.state.checked });
  console.log(this.state.checked); // becomes the opposite the state!
}

render: 渲染:

<div className="switch">
  <input className="switch-input" id="exampleSwitch" type="checkbox" onChange={this._handleChange} checked={this.state.checked} name="exampleSwitch">
  <label className="switch-paddle" htmlFor="exampleSwitch">
    <span className="show-for-sr">Download Kittens</span>
  </label>
</div>

When clicked, my react console shows it as true but in console, it's false . 单击后,我的react控制台将其显示为true但在控制台中为false The console.log() shows the opposite of the state. console.log()显示与状态相反的状态。 If the state is false then the log shows true . 如果状态为false则日志显示为true Any reason? 任何原因?

Edit for another method: 编辑另一种方法:

_onSubmit(){
 let checked = this.state.checked;
 $.ajax({
  ...
  data: {"checked": checked },
  ...
 });
}

From React documentation : 从React 文档中

setState() does not immediately mutate this.state but creates a pending state transition. setState()不会立即使this.state发生突变,但会创建一个挂起的状态转换。 Accessing this.state after calling this method can potentially return the existing value. 调用此方法后访问this.state可能会返回现有值。

So console.log() may work in unexpected manner here. 因此, console.log()可能在此处以意外方式工作。 To get proper result you probably would like to pass 2nd argument to your setState() : 为了获得正确的结果,您可能希望将第二个参数传递给setState()

The second (optional) parameter is a callback function that will be executed once setState is completed and the component is re-rendered. 第二个(可选)参数是一个回调函数,将在setState完成并重新呈现组件后执行。

_handleChange(){
  this.setState({ checked: !this.state.checked }, () => {
    console.log(this.state.checked); // output will be as expected
  });  
}

setState() is not a synchronous action and therefore the API gives you the ability to give it a callback to do something when the setState() completes. setState()不是同步操作,因此,API使您能够在setState()完成时向其回调以执行某些操作。 Example below with code that is not tested. 下面的示例未经测试的代码。 Documentation : Facebook setState() documentation. 文档: Facebook setState()文档。

 var Component = React.createClass({ getInitialState: function() { return ({ initialState: true }); }, changeState: function() { this.setState({ initialState: false }, function() { console.log(this.state.initialState); // this is the callback }); }, render: function() { return ( <button type="button" onClick = {this.changeState}> Test component </button> ); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM