Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @typechecks
10
* @providesModule cloneWithProps
11
*/
12
13
"use strict";
14
15
var ReactElement = require('ReactElement');
16
var ReactPropTransferer = require('ReactPropTransferer');
17
18
var keyOf = require('keyOf');
19
var warning = require('warning');
20
21
var CHILDREN_PROP = keyOf({children: null});
22
23
/**
24
* Sometimes you want to change the props of a child passed to you. Usually
25
* this is to add a CSS class.
26
*
27
* @param {object} child child component you'd like to clone
28
* @param {object} props props you'd like to modify. They will be merged
29
* as if you used `transferPropsTo()`.
30
* @return {object} a clone of child with props merged in.
31
*/
32
function cloneWithProps(child, props) {
33
if (__DEV__) {
34
warning(
35
!child.ref,
36
'You are calling cloneWithProps() on a child with a ref. This is ' +
37
'dangerous because you\'re creating a new child which will not be ' +
38
'added as a ref to its parent.'
39
);
40
}
41
42
var newProps = ReactPropTransferer.mergeProps(props, child.props);
43
44
// Use `child.props.children` if it is provided.
45
if (!newProps.hasOwnProperty(CHILDREN_PROP) &&
46
child.props.hasOwnProperty(CHILDREN_PROP)) {
47
newProps.children = child.props.children;
48
}
49
50
// The current API doesn't retain _owner and _context, which is why this
51
// doesn't use ReactElement.cloneAndReplaceProps.
52
return ReactElement.createElement(child.type, newProps);
53
}
54
55
module.exports = cloneWithProps;
56
57