Fragments in React Now you can render multiple DOM nodes. In order to do this, wrap it inside a parent div which essentially makes it a single DOM node with multiple elements inside it. Fragments in React do not deviate much from their literal meaning.
Answer. Yes, you can return more than one element in the render function.
React renders HTML to the web page by using a function called render(). The purpose of the function is to display the specified HTML code inside the specified HTML element. In the render() method, we can read props and state and return our JSX code to the root component of our app.
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:
render() {
return [
<li key="one">First item</li>,
<li key="two">Second item</li>,
<li key="three">Third item</li>,
<li key="four">Fourth item</li>,
];
}
Remember only to set the keys.
UPDATE
Now from the 16.2 version you can use the Fragments
render() {
return (
<React.Fragment>
<td>Hello</td>
<td>World</td>
</React.Fragment>
);
}
Short syntax
render() {
return (
<>
<td>Hello</td>
<td>World</td>
</>
);
}
In the ReactDOM.render
you still can't render multiple items because react needs a root. So you can render a single component inside the ReactDOM.render
and render an array of items in the internal component.
Since version 16.2 <React.Fragment />
(or <></>
for short) was introduced, so you can use conditions.
This is the preferable way.
return (
<React.Fragment>
<h1>Page title</h1>
<ContentComponent />
{this.props.showFooter && (
<footer>(c) stackoverflow</footer>
)}
</React.Fragment>
)
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 add key
attribute to each component in the list.
return [
<h1 key="page-title">Page</h1>,
<ContentComponent key="content" />,
<footer>(c)stackoverflow</footer>
]
In older versions of React, you can't render multiple components without wrapping them in an enclosing element (tag, component). eg:
return (
<article>
<h1>title</h1>
<meta>some meta</meta>
</article>
)
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:
React.render(
<div>
<PanelA />
<PanelB />
</div>,
document.body
);
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>
):
render () {
return (
<div>
<Thing1 />
<Thing2 />
</div>
)
}
React 16 introduced the ability to render an array of top-level elements (with a warning that they all must have unique keys):
render () {
return ([
<Thing1 key='thing-1' />,
<Thing2 key='thing-2' />
])
}
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:
import React, { Fragment } from 'react'
...
render () {
return (
<Fragment>
<Thing1 />
<Thing2 />
</Fragment>
)
}
There's a shorthand syntax for it, but it isn't supported by most tooling (e.g., syntax highlighters) yet:
import React from 'react'
...
render () {
return (
<>
<Thing1 />
<Thing2 />
</>
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With