MCPcopy Create free account
hub / github.com/krasimir/react-in-patterns / coerceRef

Function coerceRef

code/new-context-api/public/app.js:8203–8246  ·  view source on GitHub ↗
(returnFiber, current, element)

Source from the content-addressed store, hash-verified

8201var isArray$1 = Array.isArray;
8202
8203function coerceRef(returnFiber, current, element) {
8204 var mixedRef = element.ref;
8205 if (mixedRef !== null && typeof mixedRef !== 'function' && typeof mixedRef !== 'object') {
8206 {
8207 if (returnFiber.mode & StrictMode) {
8208 var componentName = getComponentName(returnFiber) || 'Component';
8209 if (!didWarnAboutStringRefInStrictMode[componentName]) {
8210 warning(false, 'A string ref, "%s", has been found within a strict mode tree. ' + 'String refs are a source of potential bugs and should be avoided. ' + 'We recommend using createRef() instead.' + '\n%s' + '\n\nLearn more about using refs safely here:' + '\nhttps://fb.me/react-strict-mode-string-ref', mixedRef, getStackAddendumByWorkInProgressFiber(returnFiber));
8211 didWarnAboutStringRefInStrictMode[componentName] = true;
8212 }
8213 }
8214 }
8215
8216 if (element._owner) {
8217 var owner = element._owner;
8218 var inst = void 0;
8219 if (owner) {
8220 var ownerFiber = owner;
8221 !(ownerFiber.tag === ClassComponent) ? invariant(false, 'Stateless function components cannot have refs.') : void 0;
8222 inst = ownerFiber.stateNode;
8223 }
8224 !inst ? invariant(false, 'Missing owner for string ref %s. This error is likely caused by a bug in React. Please file an issue.', mixedRef) : void 0;
8225 var stringRef = '' + mixedRef;
8226 // Check if previous string ref matches new string ref
8227 if (current !== null && current.ref !== null && current.ref._stringRef === stringRef) {
8228 return current.ref;
8229 }
8230 var ref = function (value) {
8231 var refs = inst.refs === emptyObject ? inst.refs = {} : inst.refs;
8232 if (value === null) {
8233 delete refs[stringRef];
8234 } else {
8235 refs[stringRef] = value;
8236 }
8237 };
8238 ref._stringRef = stringRef;
8239 return ref;
8240 } else {
8241 !(typeof mixedRef === 'string') ? invariant(false, 'Expected ref to be a function or a string.') : void 0;
8242 !element._owner ? invariant(false, 'Element ref was specified as a string (%s) but no owner was set. This could happen for one of the following reasons:\n1. You may be adding a ref to a functional component\n2. You may be adding a ref to a component that was not created inside a component\'s render method\n3. You have multiple copies of React loaded\nSee https://fb.me/react-refs-must-have-owner for more information.', mixedRef) : void 0;
8243 }
8244 }
8245 return mixedRef;
8246}
8247
8248function throwOnInvalidObjectType(returnFiber, newChild) {
8249 if (returnFiber.type !== 'textarea') {

Callers 3

updateElementFunction · 0.70
createChildFunction · 0.70
reconcileSingleElementFunction · 0.70

Calls 4

warningFunction · 0.85
getComponentNameFunction · 0.70
invariantFunction · 0.70

Tested by

no test coverage detected