發生了一些奇怪的事情。我使用foundation switch作爲我的複選框。無法獲取正確的複選框狀態
當我在反應瀏覽器工具上查看我的狀態時,我的檢查狀態爲true。當我在控制檯中記錄狀態時,它是錯誤的。錯誤在哪裏?
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!
}
渲染:
<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>
點擊時,我的反應控制檯顯示它作爲true
但在控制檯,它的false
。 console.log()
顯示與該州相反。如果狀態爲false
,則日誌顯示true
。任何原因?
編輯爲另一個方法:
_onSubmit(){
let checked = this.state.checked;
$.ajax({
...
data: {"checked": checked },
...
});
}
啊。好。但我需要知道,如果狀態是錯誤的,我可以用ajax發送。 – Sylar
謝謝。在使用ajax調用的另一種方法中,我可以安全地使用'this.state.checked'? – Sylar
@Sylar請更新你的問題,我不知道你在說什麼'另一種方法,它看起來如何等等。謝謝! –