Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
-7
rated 0 times [  0] [ 7]  / answers: 1 / hits: 43627  / 9 Years ago, fri, november 27, 2015, 12:00:00

I decided to learn React and started with the official tutorial. 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:




TypeError: Cannot read property 'props' of undefined




...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...


More From » reactjs

 Answers
57

Use function declaration ( render() {} or render: function {}) instead of arrow 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 functions are always anonymous.



[#64251] Wednesday, November 25, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;