1

I am trying to learn ReactJs and found React.NET.

Followed the tutorial on the author's website with the only change being an MVC 5 app instead of MVC 4.

Here is the jsx:

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

React.renderComponent(
  <CommentBox data={data} />,
  document.getElementById('content')
);


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">
        Hello, world! I am a CommentForm.
      </div>
    );
  }
});

var data = [
  { Author: "Daniel Lo Nigro", Text: "Hello ReactJS.NET World!" },
  { Author: "Pete Hunt", Text: "This is one comment" },
  { Author: "Jordan Walke", Text: "This is *another* comment" }
];

It gives this error:

Uncaught TypeError: undefined is not a function

Error in console

Any clues on this one?

Regards.

1
  • Looking at the tutorial, should it be <CommentList data={this.props.data} />? Commented Jun 21, 2014 at 7:35

3 Answers 3

2

There are three steps in the snippet.

First, define the CommentBox:

var CommentBox = React.createClass...

Second, render the CommentBox and the CommentList:

React.renderComponent...

Third, define the CommentList:

var CommentList = React.createClass...

So, the problem is that the CommentList is rendered before the CommentList is defined. If the last two steps were switched around then it would work fine. The CommentList class needs to be defined before it can be rendered.

Sign up to request clarification or add additional context in comments.

Comments

1

CommentForm is also defined in the wrong place - it needs to be defined before it's referred to. The React tutorial has it the wrong way round: http://facebook.github.io/react/docs/tutorial.html

The correct order for the definition of the classes in the Javascript is:

var Comment = React.createClass

...
var CommentList = React.createClass

...
var CommentForm = React.createClass

...
var CommentBox = React.createClass

...

1 Comment

And another thing the authors of the tutorial forgot to add: in the html file, instead of <script type="text/jsx"> you need <script type="text/jsx" src="/tutorial.js"> (depending on where you put tutorial.js)
0

I had this happen whilst working through the tutorial and found that declaring the var data = [...] at the top of the jsx script resolved the issue. So it would appear that the engine is not hoisting variables properly?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.