我有一個簡單的CheckBox組件:陣營簡單CheckBox組件 - 警告:的setState(...):只能更新一安裝或安裝組件
import React, { Component } from 'react';
class Checkbox extends Component {
constructor(props) {
super(props);
this.state = { isChecked: false };
this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
}
render() {
return (
<div className="checkbox">
<input type="checkbox" checked={this.state.isChecked} onChange={this.handleCheckboxChange} />
Checkbox
</div>
);
}
handleCheckboxChange(e) {
this.setState({ isChecked: e.target.checked });
}
}
export default Checkbox;
然而,切換複選框產生以下錯誤:
Warning: setState(...): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op. Please check the code for the Checkbox component.
我在創建受控組件時錯過了什麼嗎?
你有一個小提琴嗎?您的複選框組件代碼看起來沒問題。這可能與其他控件有關。 – hazardous
是啊,看起來很好,我也很好https://codepen.io/anon/pen/qXJvwm所以你問題是在某個地方的上游鏈 – varren