Uncaught TypeError: Cannot read property 'state' of undefined - React

后端 未结 2 2000
忘掉有多难
忘掉有多难 2021-01-22 05:22

I can not figure out if I am missing something small here?

Just trying to get a grasp on how state works with React.

Just creating a small check box that change

相关标签:
2条回答
  • 2021-01-22 05:47

    Changes:

    1. You forgot to bind the onChange method, either use this:

    onChange={this.handleCheck.bind(this)}

    or define the binding in the constructor:

    this.handleCheck = this.handleCheck.bind(this)

    2. You used setState in a wrong way, setState is a method you need to call it.

    Instead of: this.setState = ({})

    it should be: this.setState({})

    0 讨论(0)
  • 2021-01-22 06:00

    Binding your handleCheck function in the constructor:

    constructor(props) {
        super(props);
    
        this.state = {
            checked: true
        };
    
        this.handleCheck = this.handleCheck.bind(this);
    }
    
    0 讨论(0)
提交回复
热议问题