Add typed forwardRef example with Input component#237
Add typed forwardRef example with Input component#237The-Spik3r 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 an issue by providing a comprehensive example of how to correctly type a React component that utilizes 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. 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. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a ForwardedInput component using React.forwardRef. The implementation is a good start, but I have a couple of suggestions to improve it. First, it's better to rely on TypeScript's type inference for the ref parameter in forwardRef instead of providing an explicit, and slightly incorrect, type. This simplifies the code and improves correctness. Second, I recommend adding a displayName to the component, which is a best practice for components using forwardRef to improve the debugging experience.
| (props, ref: Ref<HTMLInputElement>) => { | ||
| return <input ref={ref} {...props} />; | ||
| } |
There was a problem hiding this comment.
The explicit type annotation ref: Ref<HTMLInputElement> is redundant and not entirely correct. The ref parameter's type is ForwardedRef<HTMLInputElement>, which is subtly different from Ref. It's best practice to let TypeScript infer this type from forwardRef. This also allows for a more concise implementation.
After applying this change, you can remove the Ref import from line 1 as it will no longer be used.
(props, ref) => <input ref={ref} {...props} />
| (props, ref: Ref<HTMLInputElement>) => { | ||
| return <input ref={ref} {...props} />; | ||
| } | ||
| ); |
Summary
This PR addresses issue #72 by fixing the correct typing of a component using
forwardrefrequires understanding both the props of the component and the type of the ref being forwarded..Changes
Testing