83

In most cases, having a parent tag isn't an issue.

React.createClass({
    render: function() {
        return (
            <tbody>
                <tr><td>Item 1</td></tr>
                <tr><td>Item 2</td></tr>
            </tbody>
        );
    }
});

But there are some cases where it makes sense to have sibling elements in one render function without a parent, and especially in the case of a table, you don't want to wrap a table row in a div.

React.createClass({
    render: function() {
        return (
            <tr><td>Item 1</td></tr>
            <tr><td>Item 2</td></tr>
        );
    }
});

The second example gives the following error: Adjacent XJS elements must be wrapped in an enclosing tag while parsing file.

How can I render two sibling elements without wrapping them in a <div> or something similar?

thealexbaron
  • 1,558
  • 1
  • 11
  • 25

9 Answers9

68

This is a limitation currently, but will likely be fixed at some point in the future (there's some open issues on the github repo).

For now, you can use a function which returns an array (this is basically a stateless component):

function things(arg, onWhatever){
    return [
        <tr><td>Item 1</td></tr>,
        <tr><td>Item 2</td></tr>
    ];
}

And use that in your component.

return (
    <table><tbody>
      {things(arg1, this.handleWhatever)}
      {things(arg2, this.handleWhatever)}
    </tbody></table>
);

Update

In React 16 you will be able to return an array from render.

Another Update

You can now either return a top level array, or use <React.Fragment>.

With an array we need to place a key on each item, as React doesn't know that the two elements are constant, instead of a dynamically created list:

function RowPair() {
  return [
    <tr key="first"><td>First</td></tr>,
    <tr key="second"><td>Second</td></tr>,
  ]
}

With React.Fragment, it behaves much more like wrapping it in a <div> or similar, where a key isn't required if we're not building the children dynamically. First, we can wrap the array in a Fragment:

function RowPair() {
  return <React.Fragment>{[
    <tr key="first"><td>First</td></tr>,
    <tr key="second"><td>Second</td></tr>,
  ]}</React.Fragment>
}

And then we can eliminate the array and keys entirely:

function RowPair() {
  return <React.Fragment>
    <tr><td>First</td></tr>
    <tr><td>Second</td></tr>
  </React.Fragment>
}
Brigand
  • 84,529
  • 20
  • 165
  • 173
  • 2
    Why does everyone say this works???? Uncaught Error: x.render(): A valid React element (or null) must be returned. You may have returned undefined, an array or some other invalid object. – Xogle Dec 05 '16 at 21:53
  • @Xogle Something must have changed in the React API. Doesn't seem to work anymore. – Petr Peller Jan 15 '17 at 22:48
  • 1
    @FakeRainBrigand I see the problem. It's not a "stateless component" it's a function returning an array. Stateless components can't return arrays and you can't connect a simple function to the Redux store. – Petr Peller Jan 16 '17 at 11:17
  • @PetrPeller are you using jsx? In the example I'm calling it like a function. – Brigand Jan 16 '17 at 17:10
  • I don't understand all the upvotes - this doesn't answer the question at all. The question is how to render sibling elements without a parent element and this answer still renders a parent element. – Dancrumb Aug 23 '17 at 16:52
  • This answer worked for me, but it did take a few minutes of rejiggering, probably because anyone with this problem is going to have a relatively complex DOM structure. – HoldOffHunger Sep 08 '17 at 19:54
  • My +1 is for the Update - I was not aware of this, and the Array-Solution is the correct answer now :) – Syndic Feb 14 '19 at 12:09
  • @Syndic thanks for reminding me about this. Updated the answer – Brigand Feb 14 '19 at 14:59
36

I know this has been an old post, but maybe my answer could be a help for newbies like me.

In React 16.2, improved support for Fragments was added.

You can now return it like this:

return (
  <>
    <tr><td>Item 1</td></tr>
    <tr><td>Item 2</td></tr>
  </>
);

You can wrap it with <></> or <Fragment></Fragment>.

If you would like to pass some attributes, only key is supported at the time of writing, and you'll have to use <Fragment /> since the short syntax <></> doesn't accept attributes.

Note: If you are going to use the short syntax, make sure that you are using Babel 7.

Source Reference

onoya
  • 2,268
  • 2
  • 17
  • 17
  • 1
    Exactly what I was after, thanks! Also, just in case anyone else needs it: import React, { Fragment } from 'react' – Chris Owens Jun 01 '18 at 05:13
  • I can't seem to find the implementation of `Fragment` in their repository, can someone, please, show me? Thanks :) – Ramon Balthazar Jun 08 '18 at 00:31
  • I can't make it work, even if I install Babel-cli... even only for the <> solution. How is it possible @onoya I'll be posting a thread soon, – Gaelle Aug 16 '18 at 12:06
16

Woohoo! The React team finally added this feature. As of React v16.0, you can do:

render() {
  // No need to wrap list items in an extra element!
  return [
    // Don't forget the keys :)
      <tr key="a"><td>Item 1</td></tr>,
      <tr key="b"><td>Item 2</td></tr>
  ];
}

See the full blog post explaining "New render return types: fragments and strings" here.

thealexbaron
  • 1,558
  • 1
  • 11
  • 25
3

Having a parent element is helpful in most cases, as for example, you can have a parent className which can target children elements style and a few other scenarios...

But, if you really don't want to do that, you can use React.Fragment

So simply do something like this:

<React.Fragment>
  <First />
  <Second />
  <Third />
</React.Fragment>

From version 16.2, there is a shortened version also using <>, which look like this in render function:

render() {
  return (
    <>
      <First />
      <Second />
      <Third />
    </>
  );
}

Also, if using version 16.0 and above, you can return array of elements which doesn't need parent wrapper also like below:

render() {
 return [
  <h1 key="heading">Hello from Alireza!</h1>,
  <p key="first">Here where I'm!</p>,
  <p key="second">And again here :)</p>
 ];
}
Alireza
  • 100,211
  • 27
  • 269
  • 172
2

We can render two sibling components by wrapping them inside React.Fragment. For e.g.

ReactDOM.render(
  <React.Fragment>
     <Item1/>
     <Item2/>
  </React.Fragment>,document.getElementById('root')
);

There is even a shorter hand for this though.

ReactDOM.render(
  <>
     <Item1/>
     <Item2/>
  </>,document.getElementById('root')
);

Wrapping the components inside the React.Fragment does not add extra nodes to DOM.

Animesh Jaiswal
  • 331
  • 3
  • 7
0

You can wrap it to the brackets like this:

React.createClass({
    render: function() {
        return (
          [
            <tr><td>Item 1</td></tr>
            <tr><td>Item 2</td></tr>
          ]
        );
    }
});
Vitalii Andrusishyn
  • 3,984
  • 1
  • 25
  • 31
0

This example is work well for me:

let values = [];

if (props.Values){
  values = [
    <tr key={1}>
      <td>props.Values[0].SomeValue</td>
    </tr>
  ,
    <tr key={2}>
        <td>props.Values[1].SomeValue</td>
        </tr>
    ];
}

return (
    <table className="no-border-table table">
      <tbody>
        <tr>
            <th>Some text</th>
        </tr>
        {values}
      </tbody>
    </table>
)
MrDuDuDu
  • 534
  • 5
  • 12
0

Something like this syntax worked for me

this.props.map((data,index)=>{return( [ <tr>....</tr>,<tr>....</tr>];)});
-2

For those, who uses TypeScript, the correct syntax is:

return [
  (
    <div>Foo</div>
  ),
  (
    <div>Bar</div>
  )
];
0leg
  • 13,464
  • 16
  • 70
  • 94