UNPKG

3.4 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9
10'use strict';
11
12var _prodInvariant = require('./reactProdInvariant');
13
14var invariant = require('fbjs/lib/invariant');
15
16/**
17 * @param {?object} object
18 * @return {boolean} True if `object` is a valid owner.
19 * @final
20 */
21function isValidOwner(object) {
22 return !!(object && typeof object.attachRef === 'function' && typeof object.detachRef === 'function');
23}
24
25/**
26 * ReactOwners are capable of storing references to owned components.
27 *
28 * All components are capable of //being// referenced by owner components, but
29 * only ReactOwner components are capable of //referencing// owned components.
30 * The named reference is known as a "ref".
31 *
32 * Refs are available when mounted and updated during reconciliation.
33 *
34 * var MyComponent = React.createClass({
35 * render: function() {
36 * return (
37 * <div onClick={this.handleClick}>
38 * <CustomComponent ref="custom" />
39 * </div>
40 * );
41 * },
42 * handleClick: function() {
43 * this.refs.custom.handleClick();
44 * },
45 * componentDidMount: function() {
46 * this.refs.custom.initialize();
47 * }
48 * });
49 *
50 * Refs should rarely be used. When refs are used, they should only be done to
51 * control data that is not handled by React's data flow.
52 *
53 * @class ReactOwner
54 */
55var ReactOwner = {
56 /**
57 * Adds a component by ref to an owner component.
58 *
59 * @param {ReactComponent} component Component to reference.
60 * @param {string} ref Name by which to refer to the component.
61 * @param {ReactOwner} owner Component on which to record the ref.
62 * @final
63 * @internal
64 */
65 addComponentAsRefTo: function (component, ref, owner) {
66 !isValidOwner(owner) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'addComponentAsRefTo(...): Only a ReactOwner can have refs. You might be adding a ref to a component that was not created inside a component\'s `render` method, or you have multiple copies of React loaded (details: https://fb.me/react-refs-must-have-owner).') : _prodInvariant('119') : void 0;
67 owner.attachRef(ref, component);
68 },
69
70 /**
71 * Removes a component by ref from an owner component.
72 *
73 * @param {ReactComponent} component Component to dereference.
74 * @param {string} ref Name of the ref to remove.
75 * @param {ReactOwner} owner Component on which the ref is recorded.
76 * @final
77 * @internal
78 */
79 removeComponentAsRefFrom: function (component, ref, owner) {
80 !isValidOwner(owner) ? process.env.NODE_ENV !== 'production' ? invariant(false, 'removeComponentAsRefFrom(...): Only a ReactOwner can have refs. You might be removing a ref to a component that was not created inside a component\'s `render` method, or you have multiple copies of React loaded (details: https://fb.me/react-refs-must-have-owner).') : _prodInvariant('120') : void 0;
81 var ownerPublicInstance = owner.getPublicInstance();
82 // Check that `component`'s owner is still alive and that `component` is still the current ref
83 // because we do not want to detach the ref if another component stole it.
84 if (ownerPublicInstance && ownerPublicInstance.refs[ref] === component.getPublicInstance()) {
85 owner.detachRef(ref);
86 }
87 }
88};
89
90module.exports = ReactOwner;
\No newline at end of file