无法将状态从子组件更改为父组件

时间:2019-04-08 08:35:32

标签: javascript reactjs

我尝试更改父组件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}          
    />
   )
}

问题:点击复选框,为什么我的状态没有改变?

1 个答案:

答案 0 :(得分:0)

复选框中提供的值存在问题,值始终相同,这就是为什么未更改的原因:

value={includeExtremeClaims}应该是value={!includeExtremeClaims}