Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-router: 'Invariant Violation: Invalid tag: {HelloWorld}', while the component is right there

I'm new to React and am trying to get up to speed with react-router (v1.0.0).

I've setup a simple component and a simple route, but it's giving me an error: Invariant Violation: Invalid tag: {HelloWorld}. You'd think it to be a clear error, but I can't figure out what's wrong with the code.

Here it is:

var HelloWorld = React.createClass({
    render: function() {
        return (
            <p>Hello world</p>
        );
    }
});

var routes = (
    <Router>
        <Route path="/" component="{HelloWorld}"/>
    </Router>
);

ReactDom.render(routes, document.querySelector('#main'));

If I switch out routes with <HelloWorld /> in the ReactDom.render statement, it works fine.

Any help is much appreciated!

like image 286
Meldon Avatar asked Nov 15 '15 09:11

Meldon


1 Answers

If you look at the documentation again, you will see that component expects a reference to a component, not a string:

component={HelloWorld}
//        ^          ^

In JSX attribute values, "..." represents a string (just like in JavaScript), and {...} represents an arbitrary JavaScript expression. Hence "{HelloWorld}" is very different from {HelloWorld}.

like image 129
Felix Kling Avatar answered Oct 17 '22 20:10

Felix Kling