未定义类型错误:无法读取未定义的属性“状态”或“道具”因此,我开始将我的应用程序从ES 2015转换为ES6,它使用了Reaction。我有一个家长班和一个孩子班,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>
);
}}儿童班,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" onClick={this.login.bind(this)}>
);
}}Child.propTypes = {
onCodeChange: React.PropTypes.func,
onLogin: React.PropTypes.func};但是,这会导致以下错误,状态未定义它指的是,if (this.state.code == "") {
// Some functionality}知道是什么导致的吗?
2 回答
MYYA
TA贡献1868条经验 获得超4个赞
家长:
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> ); }}
儿童
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}/> </div> <button id="login" onClick={this.login}> ); }}Child.propTypes = { onCodeChange: React.PropTypes.func, onLogin: React.PropTypes.func};
<Child onCodeChange={this.setCodeChange.bind(this)} onLogin={this.login.bind(this)} />
家长:
constructor(props) { super(props); this.state = { code: '' }; this.setCodeChange = this.setCodeChange.bind(this); this.login = this.login.bind(this);}
儿童
constructor(props) { super(props); this.handleCodeChange = this.handleCodeChange.bind(this); this.login = this.login.bind(this);}
汪汪一只猫
TA贡献1898条经验 获得超8个赞
<Child onCodeChange={this.setCodeChange.bind(this)} onLogin={this.login.bind(this)} />
constructor(props){ super(props); this.login = this.login.bind(this); this.setCodeChange = this.setCodeChange.bind(this);}<Child onCodeChange={this.setCodeChange} onLogin={this.login} />
添加回答
举报
0/150
提交
取消