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

javascript - React.js Error "Adjacent JSX elements must be wrapped in an enclosing tag" - Stack Overflow

programmeradmin3浏览0评论

I have the below code is react.js which is throwing an error

"Adjacent JSX elements must be wrapped in an enclosing tag". It looks like React isnt accepting same tags next to each other how do I display tabular data?

var TestRecords = React.createClass({    
  render: function() {
      return(
        <table>
          <tr>
            {this.props.records.map(record=>{
              return <td>{record.title}</td><td>record.id</td>
            }
          )}
        </tr>
      </table>
    );    
  }  
});

I have the below code is react.js which is throwing an error

"Adjacent JSX elements must be wrapped in an enclosing tag". It looks like React isnt accepting same tags next to each other how do I display tabular data?

var TestRecords = React.createClass({    
  render: function() {
      return(
        <table>
          <tr>
            {this.props.records.map(record=>{
              return <td>{record.title}</td><td>record.id</td>
            }
          )}
        </tr>
      </table>
    );    
  }  
});
Share Improve this question edited Sep 6, 2017 at 1:59 hippietrail 17k21 gold badges109 silver badges178 bronze badges asked May 1, 2016 at 5:50 JayJay 2,5277 gold badges29 silver badges37 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

With React, you may only provide two things to a ponent tree - a node (element) or a collection of nodes.

Here you're providing two nodes (two tds). You need to either wrap them in a tr, or return them as an array (with key attributes). Also less ideal in this example since it appears that your generator should probably include tr in the first place.

Try

return (
  <table>
    {this.props.records.map(record => ( // implicit return
      <tr key={record.id}>
        <td>{record.title}</td>
        <td>{record.id}</td>
      </tr>
    )}
  </table>
)

Can you try as below,

var TestRecords = React.createClass({    
  render: function() {
      return(
        <table>
          <tr>
            {this.props.records.map(record=>{
              return 
              <tr>
                <td>{record.title}</td>
                <td>record.id</td>
              </tr>
            }
          )}
        </tr>
      </table>
    );    
  }  
});

Error is because the map is trying to return two td elements. Which could be the reason for the error

I've run across that a few times, just do the following: I like to keep as much logic out of the "return" as possible. Just a preference.

var TestRecords = React.createClass({    

      render: function() {
         var trDisplay = this.props.records.map((record, idx)=>{
                                return(
                                  <tr key={idx}>
                                   <td>{record.title}</td><td>{record.id}</td>
                                   </tr>
                                    }
                        )}

                return(
                      <table>
                           {trDisplay}
                    </table>
                 );    
               }  
       });
发布评论

评论列表(0)

  1. 暂无评论