Skip to content

Commit 9d2dd81

Browse files
committed
Fix cloneElement using string ref w no owner
1 parent 7f93cb4 commit 9d2dd81

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

packages/react/src/__tests__/ReactElementClone-test.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,22 @@ describe('ReactElementClone', () => {
274274

275275
const root = ReactDOMClient.createRoot(document.createElement('div'));
276276
await act(() => root.render(<Grandparent />));
277-
expect(component.childRef).toEqual({current: null});
278-
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN');
277+
if (gate(flags => flags.enableRefAsProp && flags.disableStringRefs)) {
278+
expect(component.childRef).toEqual({current: null});
279+
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN');
280+
} else if (
281+
gate(flags => !flags.enableRefAsProp && !flags.disableStringRefs)
282+
) {
283+
expect(component.childRef).toEqual({current: null});
284+
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN');
285+
} else if (
286+
gate(flags => flags.enableRefAsProp && !flags.disableStringRefs)
287+
) {
288+
expect(component.childRef).toEqual({current: null});
289+
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN');
290+
} else {
291+
// Not going to bother testing every possible combination.
292+
}
279293
});
280294

281295
it('should overwrite props', async () => {
@@ -371,6 +385,11 @@ describe('ReactElementClone', () => {
371385
) {
372386
expect(clone.ref).toBe(element.ref);
373387
expect(clone.props).toEqual({foo: 'ef'});
388+
} else if (
389+
gate(flags => flags.enableRefAsProp && !flags.disableStringRefs)
390+
) {
391+
expect(clone.ref).toBe(element.ref);
392+
expect(clone.props).toEqual({foo: 'ef'});
374393
} else {
375394
// Not going to bother testing every possible combination.
376395
}

packages/react/src/jsx/ReactJSXElement.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,14 +866,14 @@ export function cloneElement(element, config, children) {
866866

867867
if (config != null) {
868868
if (hasValidRef(config)) {
869+
owner = ReactSharedInternals.owner;
869870
if (!enableRefAsProp) {
870871
// Silently steal the ref from the parent.
871872
ref = config.ref;
872873
if (!disableStringRefs) {
873874
ref = coerceStringRef(ref, owner, element.type);
874875
}
875876
}
876-
owner = ReactSharedInternals.owner;
877877
}
878878
if (hasValidKey(config)) {
879879
if (__DEV__) {

0 commit comments

Comments
 (0)