简体   繁体   English

React.js TypeError this.props未定义

[英]React.js TypeError this.props is undefined

I've started trying to learn react and have ran into a problem I can't seem to figure out. 我已经开始尝试学习反应,遇到了一个我似乎无法解决的问题。 Going through a tutorial making a simple comment editing web app and I'm getting this error when I try to update a comment "TypeError: _this3 is undefined", specifically on these lines: 通过制作一个简单的注释编辑Web应用程序的教程,尝试更新注释“ TypeError:_this3 is undefined”时,出现此错误,特别是在以下几行:

this.props.updateCommentText(this.refs.newText.value, this.props.index);

and this one: 还有这个:

updateCommentText={()=>this.updateComment} 

Here is the full javascript code: 这是完整的javascript代码:

class Comment extends React.Component{

  constructor(){
    super();
    this.state = {
      editing: false,
    };

    this.edit = this.edit.bind(this);
    this.save = this.save.bind(this);
  }

  edit(){
    this.setState({
      editing: true,
    });
  }

  save(){
    this.props.updateCommentText(this.refs.newText.value, this.props.index);
    this.setState({
      editing: false,
    });
  }
  remove(){
    console.log('Removing comment');
    this.props.deleteFromBoard(this.props.index)
  }

  renderNormal(){
    return (
        <div className="commentContainer">
          <div className="commentText">{this.props.children}</div>
          <button onClick={this.edit} className="button-primary">Edit</button>
          <button onClick={this.remove} className="button-danger">Remove</button>
        </div>

      );
  }

  renderForm(){
    return (
        <div className="commentContainer">
          <textarea ref="newText" defaultValue={this.props.children}></textarea>
          <button onClick={this.save} className="button-success">Save</button>
        </div>

      );
  }

  render(){
    if(this.state.editing){

      return this.renderForm();

    } else {

      return this.renderNormal();

    }
  }
}

class Board extends React.Component{

  constructor(){
    super();
    this.state = {
      comments: [
        "Hiya",
        "Awk well",
        "Boo-urns"
      ],
    }
  }

  removeComment(i){
    console.log("Removing comment: " + i);
    var arr = this.state.comments;
    arr.splice(i, 1);
    this.setState({
      comments: arr
    });
  }

  updateComment(newText, i){
    console.log("Updating comment: " + i);
    var arr = this.state.comments;
    arr[i] = newText;
    this.setState({
      comments: arr,
    });

  }

  eachComment(text, i){
    return (
      <Comment key={i} index={i} 
      updateCommentText={()=>this.updateComment} 
      deleteFromBoard={()=>this.removeComment}>

        {text}

      </Comment>
    );
  }

  render(){
    return (
        <div className="board">
          {
            this.state.comments.map(this.eachComment)
          }
        </div>

    );
  }
}



// ========================================

ReactDOM.render(
  <Board />,
  document.getElementById('container')
);

I'm assuming something has went out of scope but I'm not sure where. 我以为有些事情超出了范围,但我不确定在哪里。

This is one issue, but there may be more :) 这是一个问题,但可能还有更多:)

In this code: 在此代码中:

updateCommentText={()=>this.updateComment}

your arrow function returns a function reference, but does not call the function when updateCommentText is invoked. 您的箭头函数返回一个函数引用,但在调用 updateCommentText时不调用该函数。

Try this instead: 尝试以下方法:

updateCommentText={(value, index)=>this.updateComment(value, index)}

BTW, you have a similar issue here: 顺便说一句,您在这里有一个类似的问题:

deleteFromBoard={()=>this.removeComment}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:尝试在React.js中的父子之间使用回调时,undefined不是对象(评估&#39;this.props&#39;) - TypeError: undefined is not an object (evaluating 'this.props') when trying to use callback between child and parent in React.js 除非刷新页面,否则 React.js this.props 未定义 - React.js this.props undefined unless page is refreshed 类型错误:_This.props 未定义 - TypeError: _This.props is undefined 在React.js中使用this.props和props有什么区别? - What is the difference between using this.props and props in React.js? 类型错误:未定义不是对象(&#39;_this.props&#39;) - TypeError: undefined is not an object ('_this.props') TypeError:undefined不是对象(评估&#39;this.props&#39;) - React Native - TypeError: undefined is not an object (evaluating 'this.props') - React Native TypeError: undefined is not an object (evalating '_this.props') in my react native project - TypeError: undefined is not an object (evaluating '_this.props') in my react native project TypeError: undefined is not an object(评估'this.props = props') - TypeError: undefined is not an object (evaluating 'this.props = props') React - this.props看起来未定义 - React - this.props looks undefined React JS with Material UI - 使用 withStyles 时 this.props 未定义 - React JS with Material UI - When using withStyles this.props is undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM