Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJs - SyntaxError: embedded: Unterminated JSX contents

I am new to ReactJs and I have a stupid issue, I think, but I can't see a reason's place of it. My training code:

var ListComponent = React.createClass({
    render: function() {
        return (
            <li>{this.props.value}</li>
        );
    }
});

var TodoComponent = React.createClass({
    getInitialState: function() {
        return {
            listPoints: []
        }
    },
    addListPoint: function(event) {
        if (event.target.value !== '') {
            this.setState({
                listPoints: this.state.listPoints.push(event.target.value)
            });
        }
    },
    render: function() {
        var listPoints = [];
        for (var i=0; i<this.state.listPoints.length; i++) {
            listPoints.push(
                <ListComponent>{this.state.listPoints[i]}<ListComponent/>
            );
        }
        return (
            <ul>{listPoints}</ul>
            <input type="text" onBlur={this.addListPoint}/>
        );
    },
});


React.render(
    <TodoComponent />,
    document.getElementById('container')
);

And my traceback:

 Uncaught SyntaxError: embedded: Unterminated JSX contents (42:21)
  40 |  
  41 | React.render(
> 42 |     <TodoComponent />,
     |                      ^
  43 |     document.getElementById('container')
  44 | );
  45 | 

Every tag seems to be closed. Does someone point me to a place where the issue begun?

like image 809
krzyhub Avatar asked Nov 17 '15 12:11

krzyhub


2 Answers

You're not closing your list properly:

<ListComponent>{this.state.listPoints[i]}</ListComponent>

You wrote <ListComponent/> (a self-closing tag with no content)

Also you need to do what Kohei TAKATA says - render should have one root element (though in React 16+ you can return an array or wrap your elements in <React.Fragment>).

like image 118
Dominic Avatar answered Oct 15 '22 10:10

Dominic


Your render function of TodoComponent returns 2 elements. I think it must be one element. Please try to enclose the 2 elements by <div> or something. Like this:

<div>
    <ul>{listPoints}</ul>
    <input type="text" onBlur={this.addListPoint}/>
</div>
like image 38
Kohei TAKATA Avatar answered Oct 15 '22 11:10

Kohei TAKATA