为了账号安全,请及时绑定邮箱和手机立即绑定

未定义类型错误:无法读取未定义的属性“状态”或“道具”

未定义类型错误:无法读取未定义的属性“状态”或“道具”

缥缈止盈 2019-06-25 11:11:10
未定义类型错误:无法读取未定义的属性“状态”或“道具”因此,我开始将我的应用程序从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);}


查看完整回答
反对 回复 2019-06-25
?
汪汪一只猫

TA贡献1898条经验 获得超8个赞

我同意@Shubham Kathri给出的所有不同的解决方案,但渲染中的直接约束力除外。

不建议在呈现中直接绑定您的函数。建议您始终在构造函数中绑定它,因为如果直接在呈现中绑定,那么每当组件呈现Webpack时,就会在捆绑文件中创建一个新的函数/对象,这样Webpack包的文件大小就会增加。由于许多原因,组件重新呈现例如:执行setState,但是如果将它放置在构造函数中,则只调用一次。

不建议执行以下内容

<Child onCodeChange={this.setCodeChange.bind(this)} onLogin={this.login.bind(this)} />

始终在构造函数中执行此操作,并在需要的地方使用ref。

constructor(props){
  super(props);
  this.login = this.login.bind(this);
  this.setCodeChange = this.setCodeChange.bind(this);}<Child onCodeChange={this.setCodeChange} onLogin={this.login} />

如果您正在使用ES6,则不需要手动绑定,但如果需要,则可以。如果希望避开与范围相关的问题和手动函数/对象绑定,可以使用箭头函数。

对不起,如果我的手机上有任何输入错误


查看完整回答
反对 回复 2019-06-25
  • 2 回答
  • 0 关注
  • 1648 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信