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
* @providesModule ReactOwner
10
*/
11
12
"use strict";
13
14
var emptyObject = require('emptyObject');
15
var invariant = require('invariant');
16
17
/**
18
* ReactOwners are capable of storing references to owned components.
19
*
20
* All components are capable of //being// referenced by owner components, but
21
* only ReactOwner components are capable of //referencing// owned components.
22
* The named reference is known as a "ref".
23
*
24
* Refs are available when mounted and updated during reconciliation.
25
*
26
* var MyComponent = React.createClass({
27
* render: function() {
28
* return (
29
* <div onClick={this.handleClick}>
30
* <CustomComponent ref="custom" />
31
* </div>
32
* );
33
* },
34
* handleClick: function() {
35
* this.refs.custom.handleClick();
36
* },
37
* componentDidMount: function() {
38
* this.refs.custom.initialize();
39
* }
40
* });
41
*
42
* Refs should rarely be used. When refs are used, they should only be done to
43
* control data that is not handled by React's data flow.
44
*
45
* @class ReactOwner
46
*/
47
var ReactOwner = {
48
49
/**
50
* @param {?object} object
51
* @return {boolean} True if `object` is a valid owner.
52
* @final
53
*/
54
isValidOwner: function(object) {
55
return !!(
56
object &&
57
typeof object.attachRef === 'function' &&
58
typeof object.detachRef === 'function'
59
);
60
},
61
62
/**
63
* Adds a component by ref to an owner component.
64
*
65
* @param {ReactComponent} component Component to reference.
66
* @param {string} ref Name by which to refer to the component.
67
* @param {ReactOwner} owner Component on which to record the ref.
68
* @final
69
* @internal
70
*/
71
addComponentAsRefTo: function(component, ref, owner) {
72
invariant(
73
ReactOwner.isValidOwner(owner),
74
'addComponentAsRefTo(...): Only a ReactOwner can have refs. This ' +
75
'usually means that you\'re trying to add a ref to a component that ' +
76
'doesn\'t have an owner (that is, was not created inside of another ' +
77
'component\'s `render` method). Try rendering this component inside of ' +
78
'a new top-level component which will hold the ref.'
79
);
80
owner.attachRef(ref, component);
81
},
82
83
/**
84
* Removes a component by ref from an owner component.
85
*
86
* @param {ReactComponent} component Component to dereference.
87
* @param {string} ref Name of the ref to remove.
88
* @param {ReactOwner} owner Component on which the ref is recorded.
89
* @final
90
* @internal
91
*/
92
removeComponentAsRefFrom: function(component, ref, owner) {
93
invariant(
94
ReactOwner.isValidOwner(owner),
95
'removeComponentAsRefFrom(...): Only a ReactOwner can have refs. This ' +
96
'usually means that you\'re trying to remove a ref to a component that ' +
97
'doesn\'t have an owner (that is, was not created inside of another ' +
98
'component\'s `render` method). Try rendering this component inside of ' +
99
'a new top-level component which will hold the ref.'
100
);
101
// Check that `component` is still the current ref because we do not want to
102
// detach the ref if another component stole it.
103
if (owner.refs[ref] === component) {
104
owner.detachRef(ref);
105
}
106
},
107
108
/**
109
* A ReactComponent must mix this in to have refs.
110
*
111
* @lends {ReactOwner.prototype}
112
*/
113
Mixin: {
114
115
construct: function() {
116
this.refs = emptyObject;
117
},
118
119
/**
120
* Lazily allocates the refs object and stores `component` as `ref`.
121
*
122
* @param {string} ref Reference name.
123
* @param {component} component Component to store as `ref`.
124
* @final
125
* @private
126
*/
127
attachRef: function(ref, component) {
128
invariant(
129
component.isOwnedBy(this),
130
'attachRef(%s, ...): Only a component\'s owner can store a ref to it.',
131
ref
132
);
133
var refs = this.refs === emptyObject ? (this.refs = {}) : this.refs;
134
refs[ref] = component;
135
},
136
137
/**
138
* Detaches a reference name.
139
*
140
* @param {string} ref Name to dereference.
141
* @final
142
* @private
143
*/
144
detachRef: function(ref) {
145
delete this.refs[ref];
146
}
147
148
}
149
150
};
151
152
module.exports = ReactOwner;
153
154