Add typed forwardRef and createRef patterns - #356
Add typed forwardRef and createRef patterns#356libracapitalinvestments-rgb wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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.
| ```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 }) => ... |
There was a problem hiding this comment.
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.
| const Component = ({ children: React.ReactNode }) => ... | |
| const Component = ({ children }: { children: React.ReactNode }) => ... |
| 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> | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
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;
}
}
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