Datsik
Datsik

Reputation: 14824

React unexpected token { } when trying to loop inside render

I was following the example on the react website on how to loop inside render I've got what I wanted to accomplish done, but I have

var AdminForumContainer = React.createClass({
    getInitialState: function() {
        return { containers: [] }
    },
    componentDidMount: function() {
        $.ajax({
            method: 'GET',
            url: '/admin/manage/forum/populate',
            success: function(data) {
                console.log(data);
            }
        })  
    },
    render: function() {
        return (
            {this.state.containers.map(function(container) {
                return (
                    <table className="containers">
                    <caption>{container.containername}</caption>
                    <thead className="containerTitle">
                        <tr>
                            <td colspan="2">Main Threads</td>
                        </tr>
                    </thead>
                    <thead>
                        <tr>
                            <td>Thread Name</td>
                            <td>Delete</td>
                        </tr>
                    </thead>
                    <tbody>
                        {
                            container.mainthreads.map(function(mainthread) {
                                return (
                                <tr>
                                    <td>{mainthread.threadname}</td>
                                    <td><button className="button alert">Delete</button></td>
                                </tr>
                                )
                            })
                        }
                        <tr>
                            <td><input type="text"/></td>
                            <td><button className="button" onclick={this.createMainThread(container.containerid)}>Create</button></td>
                        </tr>
                    </tbody>
                    <thead>
                        <tr>
                            <td colspan="2">Sub Threads</td>
                        </tr>
                    </thead>
                    <tbody>
                        {
                            container.mainthreads.map(function(subthread) {
                                return (<tr>
                                    <td>{subthread.threadname}</td>
                                    <td><button className="button alert">Delete</button></td>
                                </tr>)
                            })
                        }
                        <tr>
                            <td><input type="text"/></td>
                            <td><button className="button" onclick={this.createSubThread(container.containerid)}>Create</button></td>
                        </tr>
                    </tbody>
                </table>
                )
            })}
        )
    }
});

but I get

Uncaught SyntaxError: http://localhost:8080/static/js/admin.jsx: Unexpected token (16:8)
  14 |  render: function() {
  15 |      return (
> 16 |          {this.state.containers.map(function(container) {
     |         ^
  17 |  
  18 |              <table className="containers">
  19 |                  <caption>{container.containername}</caption>

not sure what is wrong here. Thanks.

Upvotes: 1

Views: 809

Answers (1)

David L. Walsh
David L. Walsh

Reputation: 24818

That line looks OK. It's the next line that's an issue. Your loop function needs a return statement. i.e.

{this.state.containers.map(function(container) {

    return (
        <table className="containers">

Same goes for the other functions passed to Array#map.

UPDATE: I've got it. Remove the surrounding braces. They're only needed inside a JSX container. i.e.

UPDATE Mk II: In fact you need a container since React components must have a single root element. So put the whole thing in a wrapper div. i.e.

render: function () {

    return (
        <div>
            {this.state.containers.map(function(container) {

Upvotes: 1

Related Questions