Skip to content

docs: add createRef + forwardRef pattern for connected class components - #353

Open
libracapitalinvestments-rgb wants to merge 1 commit into
piotrwitek:masterfrom
libracapitalinvestments-rgb:casharmy/fix-issue-108
Open

docs: add createRef + forwardRef pattern for connected class components#353
libracapitalinvestments-rgb wants to merge 1 commit into
piotrwitek:masterfrom
libracapitalinvestments-rgb:casharmy/fix-issue-108

Conversation

@libracapitalinvestments-rgb

Copy link
Copy Markdown

Adds documentation examples showing how to safely access instance properties on connected class components using React.createRef and React.forwardRef patterns. Closes #108

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request adds documentation examples demonstrating how to access instance properties on connected class components using React.forwardRef. The feedback points out that the WrappedComponent workaround (Option 1) is broken at runtime because passing a ref to the functional component wrapper without { forwardRef: true } results in a null ref and triggers a React warning. Additionally, it is recommended to use the built-in { forwardRef: true } option in connect() instead of manually wrapping the component with React.forwardRef to reduce boilerplate.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +41 to +44
<ConnectedChild
// @ts-ignore - use WrappedComponent ref workaround
ref={this.childRef}
/>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

This approach is broken at runtime. Since ConnectedChild is a functional component wrapper created by connect(), passing a ref to it without { forwardRef: true } will trigger a React warning, and this.childRef.current will remain null. The @ts-ignore comment only silences the TypeScript compiler but does not make the ref work at runtime. It is highly recommended to remove this broken option and use the forwardRef pattern instead.

Comment on lines +35 to +42
// Wrap with forwardRef BEFORE connecting, so the ref is forwarded to the
// underlying Child class instance rather than the connected wrapper.
const ChildWithForwardedRef = React.forwardRef<Child, ChildProps>(
(props, ref) => <Child {...props} ref={ref} />
);

// Now connect the forwardRef-wrapped component
const ConnectedChild = connect()(ChildWithForwardedRef);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Instead of manually wrapping the component with React.forwardRef before connecting, you can use the built-in { forwardRef: true } option provided by react-redux's connect function. This is the standard and idiomatic way to forward refs to the wrapped component, reducing boilerplate and keeping the component definition cleaner.

Suggested change
// Wrap with forwardRef BEFORE connecting, so the ref is forwarded to the
// underlying Child class instance rather than the connected wrapper.
const ChildWithForwardedRef = React.forwardRef<Child, ChildProps>(
(props, ref) => <Child {...props} ref={ref} />
);
// Now connect the forwardRef-wrapped component
const ConnectedChild = connect()(ChildWithForwardedRef);
// Use the built-in forwardRef option in connect
const ConnectedChild = connect(
null,
null,
null,
{ forwardRef: true }
)(Child);

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.

How to safely access instance properties declared in a class component with createRef

1 participant