相关疑难解决方法(0)

未捕获的TypeError:无法读取未定义的属性"state或props"

所以我开始将我的应用程序从ES2015转换为使用React的ES6.

我有一个父类和一个像这样的子类,

export default class Parent extends Component {
    constructor(props) {
        super(props);
        this.state = {
            code: ''
        };
    }

    setCodeChange(newCode) {
        this.setState({code: newCode});
    }


    login() {
        if (this.state.code == "") {
            // Some functionality
        }
    }

    render() {
        return (
            <div>
                <Child onCodeChange={this.setCodeChange} onLogin={this.login} />
            </div>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

儿童班,

export default class Child extends Component {
    constructor(props) {
        super(props);
    }

    handleCodeChange(e) {
        this.props.onCodeChange(e.target.value);
    }

    login() {
        this.props.onLogin();
    }

    render() {
        return (
            <div>
                <input name="code" onChange={this.handleCodeChange.bind(this)}/>
            </div>
            <button id="login" …
Run Code Online (Sandbox Code Playgroud)

ecmascript-6 reactjs es6-class

6
推荐指数
1
解决办法
1119
查看次数

标签 统计

ecmascript-6 ×1

es6-class ×1

reactjs ×1