简体   繁体   English

React教程:TypeError:无法读取未定义的属性“ props”

[英]React Tutorial: TypeError: Cannot read property 'props' of undefined

I decided to learn React and started with the official tutorial. 我决定学习React,并从正式教程开始。 All is good until I get to this state of my code: 一切都很好,直到我达到代码的这种状态:

var CommentBox = React.createClass({
  render: () => {
    return (
      <div className="commentBox">
        <h1> Comments </h1>
        <CommentList />
        <CommentForm />
      </div>
    );
  }
});

var CommentForm = React.createClass({
  render: () => {
    return (
      <div className="commentForm">
        Hello, world! I am a comment form;
      </div>
    );
  }
});

var Comment = React.createClass({
  rawMarkup: () => {
    var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
    return {__html: rawMarkup};
  },

  render: () => {
    return (
      <div className="comment">
        <h2 className="commentAuthor">
          {this.props.author}
        </h2> // <--- [[[[[[ ERROR IS HERE ]]]]]]
        <span dangerouslySetInnerHtml={this.rawMarkup} />
      </div>
    );
  }
});

var CommentList = React.createClass({
  render: () => {
    return (
      <div className="commentList">
        <Comment author="Pete Hunt">This is one comment</Comment>
        <Comment author="Jordan Walke">This is *another* comment yo</Comment>
      </div>
    );
  }
});

ReactDOM.render(
  <CommentBox />,
  document.getElementById('content')
);

When I try to run it, I get the following error in devtools: 当我尝试运行它时,在devtools中出现以下错误:

TypeError: Cannot read property 'props' of undefined TypeError:无法读取未定义的属性“ props”

...and the debugger pauses at the marked line (see code). ...并且调试器在标记的行处暂停(请参见代码)。 When I mouseover this in {this.props.author} , I get a preview of the object which has the props property and everything... 当我将鼠标放置到this{this.props.author} ,我得到它具有对象的预览props属性和一切...

Use function declaration ( render() {} or render: function {} ) instead of arrow function render: () => {} 使用函数声明( render() {}render: function {} )代替箭头函数render: () => {}

var Comment = React.createClass({
  rawMarkup() {
    var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
    return {__html: rawMarkup};
  },

  render() {
    return (
      <div className="comment">
        <h2 className="commentAuthor">
          {this.props.author}
        </h2>
        <span dangerouslySetInnerHtml={this.rawMarkup} />
      </div>
    );
  }
});

Example

An arrow function expression has a shorter syntax compared to function expressions and lexically binds the this value (does not bind its own this, arguments, super, or new.target). arrow function表达式的语法比函数表达式短,并且按词法绑定此值(不绑定其自身的this,arguments,super或new.target)。 Arrow functions are always anonymous. 箭头函数始终是匿名的。

I had the same error message: 我有同样的错误信息:

Cannot read property 'props' of undefined 无法读取未定义的属性“ props”

... but from a different cause : when this is called from within a function, javascript can not reach the variable because this is in an outer scope. ... 但原因不同 :从函数内部调用this方法时,javascript无法到达变量,因为this在外部范围内。 (Note: I was in ES5) (注意:我在ES5中)

In this case, simply store this in another variable, prior to the function (in the scope of your component): var that = this; 在这种情况下,简单地存储this另一个变量,函数之前(在组件的范围内): var that = this;

Then you will be able to call that.props from within the function. 然后,您将可以从函数中调用that.props

Hope this helps for other people who had that error message. 希望这对收到该错误消息的其他人有所帮助。

Detailed example below: 详细示例如下:

 render: function() { var steps = []; var that = this; // store the reference for later use var count = 0; this.props.steps.forEach(function(step) { steps.push(<Step myFunction={function(){that.props.anotherFunction(count)}}/>); // here you are count += 1; }); return ( <div>{steps}</div> ) } 

A little late post/answer. 稍晚一些的帖子/答案。

Try to bind your function inside the constructor 尝试将函数绑定到构造函数中

example: 例:

this.yourfunction = this.yourfunction.bind(this);

我在ES6上,而箭头函数成功了:rawMarkup =()=> {}

暂无
暂无

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

相关问题 React - TypeError:无法读取未定义的属性“道具” - React - TypeError: Cannot read property 'props' of undefined 反应功能组件类型错误:无法读取未定义的属性“道具” - React functional component TypeError: Cannot read property 'props' of undefined React Redux - TypeError:无法读取未定义的属性“道具” - React Redux - TypeError: Cannot read property 'props' of undefined React Redux - Uncaught(in promise)TypeError:无法读取未定义的属性&#39;props&#39; - React Redux - Uncaught (in promise) TypeError: Cannot read property 'props' of undefined 未捕获的TypeError:无法读取react-redux中未定义的属性&#39;props&#39; - Uncaught TypeError: Cannot read property 'props' of undefined in react-redux React TypeError:在传递道具时无法读取未定义的属性“map” - React TypeError: Cannot read property 'map' of undefined on passing props TypeError:无法读取undefined的属性'firstName' - 在react中使用'props' - TypeError: Cannot read property 'firstName' of undefined - using 'props' in react React/Redux - TypeError:无法读取未定义的属性“道具” - React/Redux - TypeError: Cannot read property 'props' of undefined TypeError:无法读取 React function 中未定义的属性“道具” - TypeError: Cannot read property 'props' of undefined In a React function 未捕获的类型错误:无法读取未定义的属性“_props” - Uncaught TypeError: Cannot read property '_props' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM