Skip to content

Commit 5c3f482

Browse files
committed
Ensure class component instances have their own this.refs
1 parent 6c18429 commit 5c3f482

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

packages/react-reconciler/src/ReactChildFiber.new.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,38 @@ function coerceRef(
224224
}
225225
}
226226
}
227+
228+
if (typeof mixedRef === 'function' && element._owner) {
229+
const owner: ?Fiber = (element._owner: any);
230+
if (owner) {
231+
const ownerFiber = ((owner: any): Fiber);
232+
233+
if (ownerFiber.tag === ClassComponent) {
234+
const functionRef = mixedRef;
235+
const resolvedInst = ownerFiber.stateNode;
236+
237+
// Check if previous function ref matches new function ref
238+
if (
239+
current !== null &&
240+
current.ref !== null &&
241+
typeof current.ref === 'function' &&
242+
current.ref._functionRef === functionRef
243+
) {
244+
return current.ref;
245+
}
246+
247+
const ref = function(value) {
248+
if (resolvedInst.refs === emptyRefsObject) {
249+
// This is a lazy pooled frozen object, so we need to initialize.
250+
resolvedInst.refs = {};
251+
}
252+
functionRef.call(resolvedInst, value);
253+
};
254+
ref._functionRef = functionRef;
255+
return ref;
256+
}
257+
}
258+
}
227259
return mixedRef;
228260
}
229261

packages/react-reconciler/src/ReactChildFiber.old.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,39 @@ function coerceRef(
224224
}
225225
}
226226
}
227+
228+
if (typeof mixedRef === 'function' && element._owner) {
229+
const owner: ?Fiber = (element._owner: any);
230+
if (owner) {
231+
const ownerFiber = ((owner: any): Fiber);
232+
233+
if (ownerFiber.tag === ClassComponent) {
234+
const functionRef = mixedRef;
235+
const resolvedInst = ownerFiber.stateNode;
236+
237+
// Check if previous function ref matches new function ref
238+
if (
239+
current !== null &&
240+
current.ref !== null &&
241+
typeof current.ref === 'function' &&
242+
current.ref._functionRef === functionRef
243+
) {
244+
return current.ref;
245+
}
246+
247+
const ref = function(value) {
248+
if (resolvedInst.refs === emptyRefsObject) {
249+
// This is a lazy pooled frozen object, so we need to initialize.
250+
resolvedInst.refs = {};
251+
}
252+
functionRef.call(resolvedInst, value);
253+
};
254+
ref._functionRef = functionRef;
255+
return ref;
256+
}
257+
}
258+
}
259+
227260
return mixedRef;
228261
}
229262

packages/react/src/ReactBaseClasses.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,9 @@
77

88
import ReactNoopUpdateQueue from './ReactNoopUpdateQueue';
99
import assign from 'shared/assign';
10-
import {warnAboutStringRefs} from 'shared/ReactFeatureFlags';
1110

1211
const emptyObject = {};
13-
if (
14-
__DEV__ &&
15-
// For string refs we recommend the `string-refs` codemod that requires unsealed `this.refs`
16-
!warnAboutStringRefs
17-
) {
12+
if (__DEV__) {
1813
Object.freeze(emptyObject);
1914
}
2015

0 commit comments

Comments
 (0)