ReactOwner模块用于调用ReactCompositeComponent实例的attachRef、detachRef方法,向用户自定义组件ReactComponent实例添加或移除refs。被ReactRef调用。
'use strict'; var _prodInvariant = require('./reactProdInvariant');// 生产环境React形式带url报错 // invariant(condition,format,a,b,c,d,e,f) condition为否值,替换format中的"%s",并throw error报错 var invariant = require('fbjs/lib/invariant'); // 通过有无attachRef、detachRef方法,判断是否ReactCompositeComponent实例(用户自定义组件由其创建实例、挂载) function isValidOwner(object) { return !!(object && typeof object.attachRef === 'function' && typeof object.detachRef === 'function'); } var ReactOwner = { addComponentAsRefTo: function (component, ref, owner) { // 不是ReactCompositeComponent实例,报错 !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; // 向用户自定义组件实例添加refs owner.attachRef(ref, component); }, removeComponentAsRefFrom: function (component, ref, owner) { // 不是ReactCompositeComponent实例,报错 !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; var ownerPublicInstance = owner.getPublicInstance(); // 移除用户自定义组件实例的refs if (ownerPublicInstance && ownerPublicInstance.refs[ref] === component.getPublicInstance()) { owner.detachRef(ref); } } }; module.exports = ReactOwner;