1

I am trying to render a very simple table with react, but it complains bitterly:

"Invariant Violation: Table.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object."

Here's the definition:

var Table = React.createClass({

  render: function() {
    return 
    (<table class="table table-striped">
      <thead>
        <tr>
          <th>Header 1</th>
          <th>Header 2</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>One</td>
          <td>Two</td>
        </tr>
        <tr>
          <td>One</td>
          <td>Two</td>
        </tr>
      </tbody>
    </table>);
  }
});

It is a single element returned, the HTML is valid, so I am not sure what the problem is. Does anybody know what is the issue?

5
  • Could you provide more code? There's nothing wrong here besides class (it should be className). My suggestion is to take a look at the generated code (if using a transpilation process, and not the browser JSX tool). This error raises when: github.com/facebook/react/blob/… Commented Jan 26, 2015 at 22:18
  • Not sure what more code to provide. The component really is what you see. Changing to className didn't change anything. Commented Jan 26, 2015 at 22:26
  • yep, an error on a property wouldn't raise that error. Did you take a look at the generated code? Commented Jan 26, 2015 at 22:28
  • 5
    It's not clear if this is an artifact of copy/paste but try putting the return statement and the opening paren on the same line. I suspect you are having trouble with an automatically-inserted semicolon. Commented Jan 26, 2015 at 22:58
  • It seems that this is the answer, if you want to put it in as answer I would go on and accept it, as this fixed the issue. Commented Jan 27, 2015 at 15:06

3 Answers 3

1

Hat-tip to evan, who actually provided the answer in the comments.

I simply got tripped by the "auto"-insertion of semicolons by Javascript. The single return statement meant to return from the function, giving it a return value of undefined. the following jsx never got executed.

Starting the jsx on the same line as the return ensures that the return is executed as intended.

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

Comments

0

This is a babel compilation problem. Put html-code on its own lines.
return ( Header 1 Header 2 One Two One Two );

And change class to className

Comments

0

The return from your render method needs to nest everything under a single <div> tag.

1 Comment

no, this is not necessary, any react component will do as a root.

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.