未定义类型错误:无法读取未定义的属性“状态”或“道具”
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}
MYYA
汪汪一只猫
相关分类