-
Notifications
You must be signed in to change notification settings - Fork 47.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix cloneElement using string ref w no owner #28797
Merged
josephsavona
merged 8 commits into
facebook:main
from
josephsavona:fix-cloneelement-owner
Apr 9, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9d2dd81
Fix cloneElement using string ref w no owner
josephsavona f7b42b3
Fix tests
rickhanlonii c97b811
add repro
josephsavona b7f5119
Merge branch 'fix-cloneelement-owner' of github.com:josephsavona/reac…
josephsavona 28d13d2
fix test name
josephsavona bb30f2b
use @gate comment
josephsavona 9aedf71
undo use of at-gate since it fails in other combinations
josephsavona 5162111
maybe gate only on !disableStringRefs????
josephsavona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -274,8 +274,56 @@ describe('ReactElementClone', () => { | |
|
||
const root = ReactDOMClient.createRoot(document.createElement('div')); | ||
await act(() => root.render(<Grandparent />)); | ||
expect(component.childRef).toEqual({current: null}); | ||
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN'); | ||
if (gate(flags => flags.enableRefAsProp && flags.disableStringRefs)) { | ||
expect(component.childRef).toEqual({current: null}); | ||
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN'); | ||
} else if ( | ||
gate(flags => !flags.enableRefAsProp && !flags.disableStringRefs) | ||
) { | ||
expect(component.childRef).toEqual({current: null}); | ||
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN'); | ||
} else if ( | ||
gate(flags => flags.enableRefAsProp && !flags.disableStringRefs) | ||
) { | ||
expect(component.childRef).toEqual({current: null}); | ||
expect(component.parentRef.current.xyzRef.current.tagName).toBe('SPAN'); | ||
} else { | ||
// Not going to bother testing every possible combination. | ||
} | ||
}); | ||
|
||
// @gate !disableStringRefs | ||
it('should steal the ref if a new string ref is specified without an owner', async () => { | ||
// Regression test for this specific feature combination calling cloneElement on an element | ||
// without an owner | ||
await expect(async () => { | ||
// create an element without an owner | ||
const element = React.createElement('div', {id: 'some-id'}); | ||
class Parent extends React.Component { | ||
render() { | ||
return <Child>{element}</Child>; | ||
} | ||
} | ||
let child; | ||
class Child extends React.Component { | ||
render() { | ||
child = this; | ||
const clone = React.cloneElement(this.props.children, { | ||
ref: 'xyz', | ||
}); | ||
return <div>{clone}</div>; | ||
} | ||
} | ||
|
||
const root = ReactDOMClient.createRoot(document.createElement('div')); | ||
await act(() => root.render(<Parent />)); | ||
expect(child.refs.xyz.tagName).toBe('DIV'); | ||
}).toErrorDev([ | ||
'Warning: Component "Child" contains the string ref "xyz". Support for ' + | ||
'string refs will be removed in a future major release. We recommend ' + | ||
'using useRef() or createRef() instead. Learn more about using refs ' + | ||
'safely here: https://react.dev/link/strict-mode-string-ref', | ||
]); | ||
}); | ||
|
||
it('should overwrite props', async () => { | ||
|
@@ -371,6 +419,15 @@ describe('ReactElementClone', () => { | |
) { | ||
expect(clone.ref).toBe(element.ref); | ||
expect(clone.props).toEqual({foo: 'ef'}); | ||
} else if ( | ||
gate(flags => flags.enableRefAsProp && !flags.disableStringRefs) | ||
) { | ||
expect(() => { | ||
expect(clone.ref).toBe(element.ref); | ||
}).toErrorDev('Accessing element.ref was removed in React 19', { | ||
withoutStack: true, | ||
}); | ||
expect(clone.props).toEqual({foo: 'ef', ref: element.ref}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josephsavona since |
||
} else { | ||
// Not going to bother testing every possible combination. | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'm pretty sure this isn't running...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, not by default. thanks CI
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nah, it's getting hit, you have to run with the www variant:
Tested locally: