react-addons-create-fragment
Last updated 3 years ago by sophiebits .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install react-addons-create-fragment 
SYNC missed versions from official npm registry.

react-addons-create-fragment

Note: This is a legacy React addon, and is no longer maintained.

We don't encourage using it in new code, but it exists for backwards compatibility.
The recommended migration path is to use arrays with explicit keys on individual elements.

Importing

import createFragment from 'react-addons-create-fragment'; // ES6
var createFragment = require('react-addons-create-fragment'); // ES5 with npm

If you prefer a <script> tag, you can get it from React.addons.createFragment with:

<!-- development version -->
<script src="https://unpkg.com/react-addons-create-fragment/react-addons-create-fragment.js"></script>

<!-- production version -->
<script src="https://unpkg.com/react-addons-create-fragment/react-addons-create-fragment.min.js"></script>

In this case, make sure to put the <script> tag after React.

Overview

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:

function Swapper(props) {
  let children;
  if (props.swapped) {
    children = [props.rightChildren, props.leftChildren];
  } else {
    children = [props.leftChildren, props.rightChildren];
  }
  return <div>{children}</div>;
}

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.

To solve this problem, you can use the createFragment add-on to give keys to the sets of children.

Array<ReactNode> createFragment(object children)

Instead of creating arrays, we write:

import createFragment from 'react-addons-create-fragment';

function Swapper(props) {
  let children;
  if (props.swapped) {
    children = createFragment({
      right: props.rightChildren,
      left: props.leftChildren
    });
  } else {
    children = createFragment({
      left: props.leftChildren,
      right: props.rightChildren
    });
  }
  return <div>{children}</div>;
}

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.

The return value of createFragment should be treated as an opaque object; you can use the 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.

Current Tags

  • 15.6.0-rc.0                                ...           15-next (3 years ago)
  • 15.5.0-rc.2                                ...           dev (4 years ago)
  • 15.6.2                                ...           latest (3 years ago)

63 Versions

  • 15.6.2                                ...           3 years ago
  • 15.6.0                                ...           3 years ago
  • 15.6.0-rc.0                                ...           3 years ago
  • 15.6.0-rc.1                                ...           3 years ago
  • 15.5.4                                ...           4 years ago
  • 15.5.3 [deprecated]           ...           4 years ago
  • 15.5.2 [deprecated]           ...           4 years ago
  • 15.5.0-rc.2                                ...           4 years ago
  • 15.5.0-rc.1                                ...           4 years ago
  • 15.5.1 [deprecated]           ...           4 years ago
  • 15.5.0 [deprecated]           ...           4 years ago
  • 15.5.0-alpha.0                                ...           4 years ago
  • 16.0.0-alpha.3                                ...           4 years ago
  • 16.0.0-alpha.2                                ...           4 years ago
  • 16.0.0-alpha.0                                ...           4 years ago
  • 16.0.0-alpha                                ...           4 years ago
  • 15.4.2                                ...           4 years ago
  • 15.4.1                                ...           4 years ago
  • 15.4.0                                ...           4 years ago
  • 15.4.0-rc.4                                ...           4 years ago
  • 15.4.0-rc.3                                ...           4 years ago
  • 15.4.0-rc.2                                ...           4 years ago
  • 15.4.0-rc.1                                ...           4 years ago
  • 15.3.2                                ...           4 years ago
  • 15.3.2-rc.1                                ...           4 years ago
  • 15.3.1                                ...           4 years ago
  • 15.3.1-rc.2                                ...           4 years ago
  • 15.3.1-rc.1                                ...           4 years ago
  • 15.3.0                                ...           4 years ago
  • 15.3.0-rc.3                                ...           4 years ago
  • 15.3.0-rc.2                                ...           4 years ago
  • 15.3.0-rc.1                                ...           4 years ago
  • 15.2.1                                ...           4 years ago
  • 15.2.0                                ...           4 years ago
  • 15.2.0-rc.2                                ...           4 years ago
  • 15.2.0-rc.1                                ...           4 years ago
  • 15.1.0                                ...           5 years ago
  • 15.1.0-alpha.1                                ...           5 years ago
  • 15.0.3-alpha.2                                ...           5 years ago
  • 15.0.3-alpha.1                                ...           5 years ago
  • 15.0.2                                ...           5 years ago
  • 15.0.2-alpha.4                                ...           5 years ago
  • 15.0.2-alpha.3                                ...           5 years ago
  • 15.0.2-alpha.2                                ...           5 years ago
  • 15.0.2-alpha.1                                ...           5 years ago
  • 15.0.1                                ...           5 years ago
  • 15.0.0                                ...           5 years ago
  • 0.14.8                                ...           5 years ago
  • 15.0.0-rc.2                                ...           5 years ago
  • 15.0.0-rc.1                                ...           5 years ago
  • 0.14.7                                ...           5 years ago
  • 0.15.0-alpha.1                                ...           5 years ago
  • 0.14.6                                ...           5 years ago
  • 0.14.5                                ...           5 years ago
  • 0.14.4                                ...           5 years ago
  • 0.14.3                                ...           5 years ago
  • 0.14.2                                ...           5 years ago
  • 0.14.1                                ...           5 years ago
  • 0.14.0                                ...           5 years ago
  • 0.14.0-rc1                                ...           5 years ago
  • 0.14.0-beta3                                ...           5 years ago
  • 0.14.0-beta2                                ...           5 years ago
  • 0.14.0-beta1                                ...           5 years ago
Downloads
Today 6
This Week 1,520
This Month 6,158
Last Day 19
Last Week 1,729
Last Month 5,083
Dependencies (3)
Dev Dependencies (4)
Dependents (208)

Copyright 2014 - 2017 © taobao.org |