我尝试更改父组件onChange事件中的状态。
在点击复选框状态includeExtremeClaims
时应更改为true
。
父项:
export class ManageAnalytics extends Component {
static contextTypes = baseContextTypes
state = {
includeExtremeClaims: false
}
handleChange = (e, { name, value } = {}) => {
this.setState({ [name]: value })
}
render() {
return (
<div>
// Passing down handleChange and includeExtremeClaims state as props
<Analytics
handleChange={this.handleChange}
includeExtremeClaims={this.state.includeExtremeClaims}
/>
</div>
)
}
儿童能力:
export class Analytics extends Component {
static contextTypes = baseContextTypes
render() {
const { handleChange, includeExtremeClaims } = this.props
return (
<Checkbox
label="Include Extreme Cases"
name="includeExtremeClaims"
onChange={handleChange}
value={includeExtremeClaims}
/>
)
}
问题:点击复选框,为什么我的状态没有改变?
答案 0 :(得分:0)
复选框中提供的值存在问题,值始终相同,这就是为什么未更改的原因:
value={includeExtremeClaims}
应该是value={!includeExtremeClaims}