Javascript React 教程:TypeError:无法读取未定义的属性“道具”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33957229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
React Tutorial: TypeError: Cannot read property 'props' of undefined
提问by bitstream
I decided to learn React and started with the official tutorial. All is good until I get to this state of my code:
我决定学习 React 并从官方教程开始。一切都很好,直到我的代码达到这种状态:
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
类型错误:无法读取未定义的属性“道具”
...and the debugger pauses at the marked line (see code). When I mouseover thisin {this.props.author}, I get a preview of the object which has the propsproperty and everything...
...调试器在标记的行处暂停(见代码)。当我将鼠标悬停this在 中时{this.props.author},我会预览具有该props属性的对象以及所有内容...
回答by Alexander T.
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>
);
}
});
An
arrow functionexpression 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 functions are always anonymous.
一个
arrow function相比函数表达式表达具有较短的语法和词汇结合此值(不结合其自身的此,自变量,超级或new.target)。箭头函数总是匿名的。
回答by Fed Zee
I had the same error message:
我有同样的错误信息:
Cannot read property 'props' of undefined
无法读取未定义的属性“道具”
...but from a different cause: when thisis called from within a function, javascript can not reach the variable because thisis in an outer scope.(Note: I was in ES5)
...但出于不同的原因:当this从函数内部调用时,javascript 无法访问该变量,因为this它在外部范围内。(注意:我在 ES5 中)
In this case, simply store thisin 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.propsfrom 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>
)
}
回答by Jing Daradal
A little late post/answer.
有点晚的帖子/答案。
Try to bind your function inside the constructor
尝试在构造函数中绑定你的函数
example:
例子:
this.yourfunction = this.yourfunction.bind(this);
回答by Master Ace
I am on ES6 and the arrow function did the trick: rawMarkup = () => {}
我在 ES6 上,箭头函数成功了:rawMarkup = () => {}

