For example could I do:
import React from 'react';
import PanelA from './panelA.jsx';
import PanelB from './panelB.jsx';
React.render(
<PanelA />
<PanelB />,
document.body
);
where React would render:
body
PanelA
PanelB
Currently I'm getting the error:
Adjacent JSX elements must be wrapped in an enclosing tag
while transpiling with browserify and babelify
React >= 16.2
Since version 16.2 you can use
<React.Fragment />
, so you can use conditions. This is preferable way.React 16
Since React 16, you can return from the
render()
method a list of components. The trade of is you cant easily condition the render and need to addkey
attribute to each component in the list.React < 16
In older versions of React, you can't render multiple components without wrapping them in an enclosing element (tag, component). eg:
If you want to use them realy as just one element, you have to create a module from them.
Just wrap your multiple components into single tag. For example:
Since the React v16.0 release you can render an array of components without wrapping items in an extra element when you are inside a component:
Remember only to set the keys.
In the
ReactDOM.render
you still can't render multiple items because react needs a root. So you can render a single component inside theReactDOM.render
and render an array of items in the internal component.Prior to React 16, multiple top-level elements in the same
render()
would require you to wrap everything in a parent element (typically a<div>
):React 16 introduced the ability to render an array of top-level elements (with a warning that they all must have unique keys):
React 16.2 introduced the
<Fragment>
element, which functions exactly like the<div>
in the first example but doesn't leave a pointless parent<div>
hanging around the DOM:There's a shorthand syntax for it, but it isn't supported by most tooling (e.g., syntax highlighters) yet:
If you wish to render multiple components out you need to nest them within one another in order to maintain the Tree-Like structure. This is explained on their docs page for Multiple Components
Ultimately as long as there is one Node at the top level it can work.
You could use just one DOM element such as a
<div>
However as you create more complex apps and have more interlinking components you may find it best to wrap child components in a parent like so
And then in your main js file, you would do: