fix: ref in functional components - #38
Open
OliverGeneser wants to merge 2 commits into
Open
Conversation
OliverGeneser
marked this pull request as draft
September 2, 2026 09:03
OliverGeneser
marked this pull request as ready for review
September 2, 2026 09:04
ThiefMaster
reviewed
Sep 2, 2026
| const Overridden = overriddenComponents[id]; | ||
| const element = React.createElement(Overridden, {...childProps, ...restProps}); | ||
| const props = {...childProps, ...restProps}; | ||
| if (ref) props.ref = ref; |
Member
There was a problem hiding this comment.
huh, looks like we don't use eslint here. i guess i pretend to be the linter then... ;)
Suggested change
| if (ref) props.ref = ref; | |
| if (ref) { | |
| props.ref = ref; | |
| } |
| } else if (child) { | ||
| // No override? Clone the Overridable component's original children | ||
| const element = React.cloneElement(child, childProps); | ||
| const element = ref ? React.cloneElement(child, {ref}) : React.cloneElement(child, childProps); |
Member
There was a problem hiding this comment.
shouldn't childProps be included in the cloneElement as well?
Contributor
Author
There was a problem hiding this comment.
From what I see in the React Docs cloneElement preserves the element's existing props and merges the provided props, so passing {ref} will retain the childProps. And now that I think about it, it shouldn't be needed in the other cloneElement either 🤔
OliverGeneser
force-pushed
the
fix-forward-ref
branch
from
September 2, 2026 15:26
9da6ba8 to
1d48459
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Heads up, the test is AI-generated.
This fixes refs in functional components using forwardRef, so ref's doesn't return null. It should work from React 16.3 all the way through React 19, although forwardRef is deprecated in React 19 and will be removed in a future release.