You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.3 KiB
65 lines
2.3 KiB
10 years ago
|
---
|
||
|
id: create-fragment
|
||
|
title: Keyed Fragments
|
||
9 years ago
|
permalink: docs/create-fragment.html
|
||
8 years ago
|
layout: docs
|
||
|
category: Add-Ons
|
||
10 years ago
|
---
|
||
|
|
||
8 years ago
|
**Importing**
|
||
|
|
||
|
```javascript
|
||
8 years ago
|
import createFragment from 'react-addons-create-fragment'; // ES6
|
||
|
var createFragment = require('react-addons-create-fragment'); // ES5 with npm
|
||
8 years ago
|
```
|
||
|
|
||
|
## Overview
|
||
|
|
||
10 years ago
|
In most cases, you can use the `key` prop to specify keys on the elements you're returning from `render`. However, this breaks down in one situation: if you have two sets of children that you need to reorder, there's no way to put a key on each set without adding a wrapper element.
|
||
|
|
||
|
That is, if you have a component such as:
|
||
|
|
||
|
```js
|
||
9 years ago
|
function Swapper(props) {
|
||
8 years ago
|
let children;
|
||
9 years ago
|
if (props.swapped) {
|
||
|
children = [props.rightChildren, props.leftChildren];
|
||
|
} else {
|
||
|
children = [props.leftChildren, props.rightChildren];
|
||
10 years ago
|
}
|
||
9 years ago
|
return <div>{children}</div>;
|
||
|
}
|
||
10 years ago
|
```
|
||
|
|
||
|
The children will unmount and remount as you change the `swapped` prop because there aren't any keys marked on the two sets of children.
|
||
|
|
||
9 years ago
|
To solve this problem, you can use the `createFragment` add-on to give keys to the sets of children.
|
||
10 years ago
|
|
||
9 years ago
|
#### `Array<ReactNode> createFragment(object children)`
|
||
10 years ago
|
|
||
|
Instead of creating arrays, we write:
|
||
|
|
||
8 years ago
|
```javascript
|
||
|
import createFragment from 'react-addons-create-fragment';
|
||
9 years ago
|
|
||
9 years ago
|
function Swapper(props) {
|
||
8 years ago
|
let children;
|
||
9 years ago
|
if (props.swapped) {
|
||
|
children = createFragment({
|
||
|
right: props.rightChildren,
|
||
|
left: props.leftChildren
|
||
|
});
|
||
|
} else {
|
||
|
children = createFragment({
|
||
|
left: props.leftChildren,
|
||
|
right: props.rightChildren
|
||
|
});
|
||
|
}
|
||
|
return <div>{children}</div>;
|
||
10 years ago
|
}
|
||
|
```
|
||
|
|
||
|
The keys of the passed object (that is, `left` and `right`) are used as keys for the entire set of children, and the order of the object's keys is used to determine the order of the rendered children. With this change, the two sets of children will be properly reordered in the DOM without unmounting.
|
||
|
|
||
7 years ago
|
The return value of `createFragment` should be treated as an opaque object; you can use the [`React.Children`](/docs/react-api.html#react.children) helpers to loop through a fragment but should not access it directly. Note also that we're relying on the JavaScript engine preserving object enumeration order here, which is not guaranteed by the spec but is implemented by all major browsers and VMs for objects with non-numeric keys.
|