Fix: How to safely access instance properties declared in a class component w#229
Fix: How to safely access instance properties declared in a class component w#229obsofficer-ctrl wants to merge 1 commit intopiotrwitek:masterfrom
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a common challenge in React-Redux applications involving TypeScript and refs. It provides a clear, type-safe pattern for accessing instance methods of a class component that has been wrapped by react-redux's connect higher-order component. By correctly configuring connect with forwardRef and structuring the component exports, the solution ensures that React.createRef resolves to the actual component instance, enabling reliable interaction with its properties and methods. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This PR adds a useful example for using forwardRef with react-redux's connect HOC. The implementation is correct and follows the intended pattern. I've added a couple of suggestions to further improve the code by applying some common React best practices for type safety and performance in class components.
|
|
||
| // ─── Parent ─────────────────────────────────────────────────────────────────── | ||
|
|
||
| export class Parent extends React.Component { |
There was a problem hiding this comment.
For better type safety, it's recommended to explicitly type the props for class components. Since the Parent component doesn't accept any props, you can define it as React.Component<{}>. Using React.Component without generics defaults props to any, which can hide potential bugs.
| export class Parent extends React.Component { | |
| export class Parent extends React.Component<{}> { |
| bar() { | ||
| if (this.childRef.current) { | ||
| // ✅ No TypeScript error – .foo() is correctly typed | ||
| this.childRef.current.foo(); | ||
| } | ||
| } |
There was a problem hiding this comment.
To avoid creating a new function in the render method for the onClick handler, you can define bar as a class property using an arrow function. This binds this correctly and allows you to pass the method directly to onClick (i.e., <button onClick={this.bar}>). This is a common performance optimization in React class components. I've also made the method private as it's an internal helper.
| bar() { | |
| if (this.childRef.current) { | |
| // ✅ No TypeScript error – .foo() is correctly typed | |
| this.childRef.current.foo(); | |
| } | |
| } | |
| private bar = () => { | |
| if (this.childRef.current) { | |
| // ✅ No TypeScript error – .foo() is correctly typed | |
| this.childRef.current.foo(); | |
| } | |
| }; |
Summary
This PR addresses: #108
Task: How to safely access instance properties declared in a class component with createRef
Platform: github
Changes
Solution: Safely Accessing Instance Properties with
createRefon Connected ComponentsAnalysis
The core problem is that
React.createRef<Child>()creates a ref typed to the unwrappedChildclass, but when you useconnect()(Child), the exported component is aConnectedComponentClasswrapper. TypeScript doesn't know that therefon the connected component will actually point to the innerChildinstance.The solution is to use the
WrappedComponentRef/ConnectedComponentClassinstance type trick, or more cleanly: export the inner class separately and type the ref against the inner class directly, while usingReact.createRef<Child>()with the unwrapped class type.Let me provide a complete working example that fits the repo's playground style.
Implementation