最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ReactJs - SyntaxError: embedded: Unterminated JSX contents - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question asked Nov 17, 2015 at 12:08 krzyhubkrzyhub 6,54011 gold badges43 silver badges70 bronze badges 1
  • How are you transforming the JSX into JS? Webpack + babel? – Tom Commented Nov 17, 2015 at 12:20
Add a comment  | 

2 Answers 2

Reset to default 15

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>).

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>
发布评论

评论列表(0)

  1. 暂无评论