Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  128] [ 6]  / answers: 1 / hits: 21485  / 9 Years ago, wed, september 9, 2015, 12:00:00

I'm following React tutorial at: http://facebook.github.io/react/docs/tutorial.html



I'm just after http://facebook.github.io/react/docs/tutorial.html#fetching-from-the-server



I went through similar questions on SO but not found a solution for my specific case.



var data = [
{author: Pete Hunt, text: This is one comment},
{author: Jordan Walke, text: This is *another* comment},
{author: Bob Lilly, text: This is *another* comment 2}

];

var Comment = React.createClass({
render: function() {
var rawMarkup = marked(this.props.children.toString(), {sanitize: true});
return (
<div className=comment>
<h2 className=commentAuthor>
{this.props.author}
</h2>
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
</div>
);
}
});

var CommentList = React.createClass({
render: function() {
var commentNodes = this.props.data.map(function (comment) {
return (
<Comment author={comment.author}>
{comment.text}
</Comment>
);
});
return (
<div className=commentList>
{commentNodes}
</div>
);
}
});

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

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


React.render(
// <CommentBox url=comments.json />,
<CommentBox data={data} />,
document.getElementById('content')
);


When I try to use data got from server (first step --> see 2nd link), I get this error:




Uncaught TypeError: Cannot read property 'data' of null




I guess this has something to do with passing data the wrong way.



EDIT: I edited the cod with the answers given so far



EDIT 2: Now it works with dumb data (var data = [ ... ) but not when got from the server


More From » reactjs

 Answers
5

Just adding that line will not get data from the server. You need to work all the way down to the end of the Reactive State section, creating the data file, and adding some ajax code to load the data.


[#65122] Tuesday, September 8, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
zaynerogerb

Total Points: 454
Total Questions: 109
Total Answers: 97

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
zaynerogerb questions
;