-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Add docs for createFragment #3353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| --- | ||
| id: create-fragment | ||
| title: Keyed Fragments | ||
| permalink: create-fragment.html | ||
| prev: clone-with-props.html | ||
| next: update.html | ||
| --- | ||
|
|
||
| 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 | ||
| var Swapper = React.createClass({ | ||
| propTypes: { | ||
| // `leftChildren` and `rightChildren` can be a string, element, array, etc. | ||
| leftChildren: React.PropTypes.node, | ||
| rightChildren: React.PropTypes.node, | ||
|
|
||
| swapped: React.PropTypes.bool | ||
| } | ||
| render: function() { | ||
| var children; | ||
| if (this.props.swapped) { | ||
| children = [this.props.rightChildren, this.props.leftChildren]; | ||
| } else { | ||
| children = [this.props.leftChildren, this.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 `React.addons.createFragment` to give keys to the sets of children. | ||
|
|
||
| #### `ReactFragment React.addons.createFragment(object children)` | ||
|
|
||
| Instead of creating arrays, we write: | ||
|
|
||
| ```js | ||
| if (this.props.swapped) { | ||
| children = React.addons.createFragment({ | ||
| right: this.props.rightChildren, | ||
| left: this.props.leftChildren | ||
| }); | ||
| } else { | ||
| children = React.addons.createFragment({ | ||
| left: this.props.leftChildren, | ||
| right: this.props.rightChildren | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| > **Note:** | ||
| > | ||
| > In the future, `createFragment` may be replaced by an API such as | ||
| > | ||
| > ```js | ||
| > return ( | ||
| > <div> | ||
| > <x:frag key="right">{this.props.rightChildren}</x:frag>, | ||
| > <x:frag key="left">{this.props.leftChildren}</x:frag> | ||
| > </div> | ||
| > ); | ||
| > ``` | ||
| > | ||
| > allowing you to assign keys directly in JSX without adding wrapper elements. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This last bit is a bit confusing. Perhaps it's just the "this may use an API such as" after explaining that createFragment is the API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future,
createFragmentmay be replaced by an API such asThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I might still lose the
x:inx:frag, though it doesn't really matter.