Skip to content

fix: ref in functional components - #38

Open
OliverGeneser wants to merge 2 commits into
indico:masterfrom
OliverGeneser:fix-forward-ref
Open

fix: ref in functional components#38
OliverGeneser wants to merge 2 commits into
indico:masterfrom
OliverGeneser:fix-forward-ref

Conversation

@OliverGeneser

@OliverGeneser OliverGeneser commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

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.

@OliverGeneser
OliverGeneser marked this pull request as draft September 2, 2026 09:03
@OliverGeneser
OliverGeneser marked this pull request as ready for review September 2, 2026 09:04
Comment thread src/overridable.js Outdated
const Overridden = overriddenComponents[id];
const element = React.createElement(Overridden, {...childProps, ...restProps});
const props = {...childProps, ...restProps};
if (ref) props.ref = ref;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Comment thread src/overridable.js
} 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);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't childProps be included in the cloneElement as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 🤔

@kpsherva kpsherva left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants