Skip to content

Add typed forwardRef and createRef patterns - #356

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

Add typed forwardRef and createRef patterns#356
libracapitalinvestments-rgb wants to merge 1 commit into
piotrwitek:masterfrom
libracapitalinvestments-rgb:casharmy/fix-issue-72

Conversation

@libracapitalinvestments-rgb

Copy link
Copy Markdown

Adds documentation examples showing how to properly type React.forwardRef and React.createRef with TypeScript. Includes basic forwardRef usage and a pattern for connected (Redux) components. Closes #72

@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 refactors and simplifies the React & Redux in TypeScript guide (README_SOURCE.md), streamlining it into three core sections and adding new component examples for createRef and forwardRef patterns. The review feedback highlights a syntax error in the README's destructuring example and points out that the TestComponent implementation in create-ref-example.tsx contains unused state and does not match the README example, which could trigger compiler or linter errors.

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 thread README_SOURCE.md
```tsx
const elementOrPrimitive: React.ReactNode = 'string' || 0 || false || null || undefined || <div /> || <MyComponent />;
const elementOrPrimitive: React.ReactNode = 'Hello!' || 0 || false || null || undefined || <div /> || <MyComponent />;
const Component = ({ children: React.ReactNode }) => ...

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

In TypeScript/JavaScript destructuring, { children: React.ReactNode } attempts to rename the children property to a variable named React.ReactNode. Because of the dot (.), this is a syntax error. It should be typed as an object type instead.

Suggested change
const Component = ({ children: React.ReactNode }) => ...
const Component = ({ children }: { children: React.ReactNode }) => ...

Comment on lines +7 to +30
type State = {
count: number;
};

class TestComponent extends React.Component<Props, State> {
private divRef = React.createRef<HTMLDivElement>();

state: State = {
count: 0,
};

getCounterValue() {
return this.state.count;
}

render() {
const { children } = this.props;
return (
<div ref={this.divRef}>
{children}
</div>
);
}
}

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

The TestComponent implementation does not match the example provided in the README_SOURCE.md. It includes an unused State type and state property, while the divRef is never read, which will trigger unused variable/member compiler or linter errors. Let's update it to match the README example exactly.

class TestComponent extends React.Component<Props> {
  private divRef = React.createRef<HTMLDivElement>();

  render() {
    const { children } = this.props;
    return (
      <div ref={this.divRef}>
        {children}
      </div>
    );
  }

  getDiv() {
    return this.divRef.current;
  }
}

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 use forwardRef?

1 participant