Return multiple React elements in a method without a wrapper element

前端 未结 10 943
南方客
南方客 2020-12-16 13:06

I\'m trying to return multiple React elements from a helper method. I could solve it simply by moving around some code, but I\'m wondering if there\'s a cleaner way to solve

10条回答
  •  伪装坚强ぢ
    2020-12-16 13:41

    This is a bit hacky but it doesn't have unnecessary jsx as you wished.

    var author = 'Daniel';
    var title = 'Hello';
    
    var Hello = React.createClass({
      _renderAutho0r: function() {
        if (!author) {
          return null;
        }
    
        return {author}
    },
    
        render: function() {
        var by = author ? ' by ' : null;
    
            return (
          
    {title} {by} {this._renderAutho0r()}
    ); } }); React.render(, document.body);

    my JSFiddle

提交回复
热议问题