Skip to content

Add typed forwardRef example with Input component#237

Open
The-Spik3r wants to merge 1 commit intopiotrwitek:masterfrom
The-Spik3r:fix-issue-1773930479035
Open

Add typed forwardRef example with Input component#237
The-Spik3r wants to merge 1 commit intopiotrwitek:masterfrom
The-Spik3r:fix-issue-1773930479035

Conversation

@The-Spik3r
Copy link
Copy Markdown

Summary

This PR addresses issue #72 by fixing the correct typing of a component using forwardref requires understanding both the props of the component and the type of the ref being forwarded..

Changes

  • Improved type definitions and implementation
  • Added proper generic type handling
  • Enhanced code maintainability

Testing

  • Code follows project style guidelines
  • Tests pass locally
  • Ready for review

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 forwardRef. The changes focus on demonstrating proper generic type handling for both component props and the forwarded ref, thereby enhancing type safety and overall code maintainability for components that need to expose their DOM nodes.

Highlights

  • Typed forwardRef Example: Introduced a new component, ForwardedInput, to demonstrate the correct typing of React's forwardRef with an HTML input element.
  • Improved Type Definitions: Implemented robust type definitions for forwardRef usage, ensuring proper generic type handling for component props and ref types.
  • Enhanced Code Maintainability: Provided a clear and correctly typed example that improves code maintainability and reduces potential type-related errors when using forwardRef.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

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 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.

Comment on lines +6 to +8
(props, ref: Ref<HTMLInputElement>) => {
return <input ref={ref} {...props} />;
}
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 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} />;
}
);
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

To improve debuggability with React DevTools, it's a good practice to add a displayName to components created with forwardRef.

);

ForwardedInput.displayName = 'ForwardedInput';

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.

1 participant