Andrew Clark 7 years ago
parent
commit
698543b5c1
  1. 63
      content/blog/2017-11-28-react-v16.2.0-fragment-support.md

63
content/blog/2017-11-28-react-v16.2.0-fragment-support.md

@ -1,5 +1,5 @@
--- ---
title: "React v16.2.0: Fragment Support" title: "React v16.2.0: Improved Support for Fragments"
author: [clemmy] author: [clemmy]
--- ---
@ -19,15 +19,18 @@ render() {
} }
``` ```
This exciting new feature is made possible by new additions to both React and JSX.
## What Are Fragments? ## What Are Fragments?
A common pattern is for a component to return a list of children. An example is rendering some text with a link inside. Take this example HTML: A common pattern is for a component to return a list of children. Take this example HTML:
```html ```html
Text with Some text.
<a href="/foo">multiple</a> <h2>A heading</h2>
<a href="/bar">links</a> More text.
inside it. <h2>Another heading</h2>
Even more text.
``` ```
Prior to version 16, the only way to acheive this in React was by wrapping the children in an extra element, usually a `div` or `span`: Prior to version 16, the only way to acheive this in React was by wrapping the children in an extra element, usually a `div` or `span`:
@ -35,14 +38,14 @@ Prior to version 16, the only way to acheive this in React was by wrapping the c
```js ```js
render() { render() {
return ( return (
// Extraneous span element :( // Extraneous div element :(
<span> <div>
Text children Some text.
<button>interleaved</button> <h2>A heading</h2>
with More text.
<button>other</button> <h2>Another heading</h2>
children. Even more text.
</span> </div>
); );
} }
``` ```
@ -52,11 +55,11 @@ To address this limitation, React 16.0 added support for [returning an array of
```jsx ```jsx
render() { render() {
return [ return [
"Text children", "Some text.",
<button key="button1">interleaved</button>, <h2 key="heading-1">A heading</h2>,
"with", "More text."
<button key="button2">other</button>, <h2 key="heading-2">Another heading</h2>,
"children." "Even more text."
]; ];
} }
``` ```
@ -73,11 +76,11 @@ To provide a more consistent authoring experience for fragments, React now provi
render() { render() {
return ( return (
<Fragment> <Fragment>
Text children Some text.
<button>interleaved</button> <h2>A heading</h2>
with More text.
<button>other</button> <h2>Another heading</h2>
children. Even more text.
</Fragment> </Fragment>
); );
} }
@ -112,11 +115,11 @@ Fragments are a common pattern in our codebases at Facebook. We anticipate they'
render() { render() {
return ( return (
<> <>
Text children Some text.
<button>interleaved</button> <h2>A heading</h2>
with More text.
<button>other</button> <h2>Another heading</h2>
children. Even more text.
</> </>
); );
} }
@ -152,7 +155,7 @@ function Glossary(props) {
### Live Demo ### Live Demo
You can experiment with JSX fragment syntax with this [CodePen](#). You can experiment with JSX fragment syntax with this [CodePen](https://codepen.io/reactjs/pen/VrEbjE?editors=1000).
## Support for Fragment Syntax ## Support for Fragment Syntax

Loading…
Cancel
Save