mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
dd61439061
I still think the semantics of flattening children is valid but we'll want to revert the flattening implementation while we solidify the semantics and try another approach. This reverts flattening so that this.props.children can once again be either a single child, flat array or nested array. mapChildren calls flattenChildren before doing anything else. This is not the most efficient approach but I wanted to keep this inital diff simple. It also ignores empty values for backwards compatibility. We may want to try another approach where empty values are included in the map. Validation of keys is still done inside ReactComponent. Ideally I'd like to extract that into a separate module but to avoid cyclic dependencies, I'm keeping it in ReactComponent for now.
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
/**
|
|
* Copyright 2013 Facebook, Inc.
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*
|
|
* @providesModule onlyChild
|
|
*/
|
|
"use strict";
|
|
|
|
var ReactComponent = require('ReactComponent');
|
|
|
|
var invariant = require('invariant');
|
|
|
|
/**
|
|
* Returns the first child in a collection of children and verifies that there
|
|
* is only one child in the collection. The current implementation of this
|
|
* function assumes that a single child gets passed without a wrapper, but the
|
|
* purpose of this helper function is to abstract away the particular structure
|
|
* of children.
|
|
*
|
|
* @param {?object} children Child collection structure.
|
|
* @return {ReactComponent} The first and only `ReactComponent` contained in the
|
|
* structure.
|
|
*/
|
|
function onlyChild(children) {
|
|
invariant(
|
|
ReactComponent.isValidComponent(children),
|
|
'onlyChild must be passed a children with exactly one child.'
|
|
);
|
|
return children;
|
|
}
|
|
|
|
module.exports = onlyChild;
|